易失性过载? [英] volatile overloading?

查看:108
本文介绍了易失性过载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说volatile是const的重载因素。

I heard that volatile is factor of overloading like const.

如果一个函数被volatile参数重载,
当是volatile-version被调用?

If a function is overloaded by volatile parameter, when is the volatile-version called?

我无法想象调用volatile版本时的情况。

I can't imagine a situation when the volatile-version is called. 

推荐答案

易失性可以应用于参数,但它不直接应用于参数时是过载的因素。然而,可以使用它来区分参数的类型。例如,这是合法的:

Volatile can be applied to parameters, but it is not a factor of overloading when applied directly to the parameter. It is however possible to use it to distinguish types of the parameter. For example, this is legal:

void f(int &p) {}; //reference to int
void f(volatile int &p) {}; //reference to volatile int

这不是:

void f(int p) {};
void f(volatile int p) {};

原因是在第一个例子中,引用不是volatile,而是整数。在第二个示例中,两种类型都是整数,因此类型相同。

The reason is that in the first example the reference is not what is volatile, but the integer. In the second example, both types are integers and therefore the same type.

还有volatile方法。它们类似于声明这个是不稳定的。因为这个是一个指针,而不是包含类型本身,以下也是合法的:

There are also volatile methods. They are akin to declaring this to be volatile. Because this is a pointer and not the containing type itself, the following is also legal:

void c::f(int p) {};
void c::f(int p) volatile {};

const

C ++标准的相关部分是§13.1<可重载声明。从C ++ 11草案n3290:

This relevant part of the C++ standard is §13.1 Overloadable declarations. From C++11 draft n3290:


仅在const和/或volatile的存在或不存在时有所不同的参数声明是等价的。也就是说,当确定正在声明,定义或调用哪个函数时,将忽略每个参数类型的const和volatile类型说明符。 [Example:

Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called. [ Example:



typedef const int cInt;
int f(int);
int f(const int);          // redeclaration of f(int)
int f(int) { /* ... */ }   // definition of f(int)
int f(cInt) { /* ... */ }  // error: redefinition of f(int)




- end示例]

— end example ]

只有在参数类型规范最外层的const和volatile类型说明符以这种方式被忽略;隐含在参数类型规范中的const和volatile类型说明符是重要的,并且可以用于区分重载函数声明 124 。特别是对于任何类型T,指向T 的指针,指向const T 的指针和指向volatile T 的指针被视为不同的参数类型,引用T 引用const T 引用volatile T

Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations124. In particular, for any type T, pointer to T, pointer to const T, and pointer to volatile T are considered distinct parameter types, as are reference to T, reference to const T, and reference to volatile T.

124)当参数类型包括函数类型,例如在参数类型是指向函数的指针的情况下,内部函数类型的参数类型规范的最外层的
const和volatile类型说明符是
也被忽略。

这篇关于易失性过载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆