C ++ 98/03参考崩溃和cv限定符 [英] C++98/03 reference collapsing and cv qualifiers
问题描述
下面的代码编译(gcc 4.7.2或icc 13)并生成1 2输出。这意味着 const
限定符被丢弃,即, f
int&
。
The code below compiles (gcc 4.7.2 or icc 13) and produces "1 2" output. Which means that const
qualifier is dropped, i. e., f<int&>
has the parameter type int&
.
为什么会发生?据我理解,根据§14.3.1.4:
Why does it happen? As I understand, according to §14.3.1.4:
如果模板参数
T c c c c c
T
创建 cv12S
cv12 是cv属性 cv1 和 cv2 的联合。忽略冗余cv-quali文件。
If a template-argument for a template-parameter
T
names a type "reference to cv1S
", an attempt to create the type "reference to cv2T
" creates the type "reference to cv12S
", where cv12 is the union of the cv-qualifiers cv1 and cv2. Redundant cv-qualifiers are ignored.
const
不应删除。这里是代码:
const
should not be dropped. Here is the code:
#include <iostream>
using namespace std;
template <typename T>
void f(const T& t)
{
t++;
}
int main()
{
int a = 1;
cout << a;
f<int&>(a);
cout << ' ' << a << endl;
return 0;
}
推荐答案
似乎起源于:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1770.html
14.3.1 - 模板类型参数
14.3.1 - Template type arguments
-4-如果模板参数T名称的模板参数类型lvalue-reference to cv1 S,尝试创建类型(lvalue或rvalue)对cv2 T的引用创建类型lvalue-reference to cv12 S,其中cv12是cv-qualifiers cv1和cv2。如果模板参数名称类型为rvalue-reference to cv1 S,则尝试创建类型lvalue-reference to cv2 T将创建类型lvalue-reference to cv12 S如果模板参数名称类型为rvalue-reference to cv1 S,则尝试创建类型rvalue-reference to cv2 T将创建类型rvalue-reference to cv12 S
-4- If a template-argument for a template-parameter T names a type "lvalue-reference to cv1 S," an attempt to create the type "(lvalue or rvalue) reference to cv2 T" creates the type "lvalue-reference to cv12 S," where cv12 is the union of the cv-qualifiers cv1 and cv2. If the template-argument names a type "rvalue-reference to cv1 S," an attempt to create the type "lvalue-reference to cv2 T" creates the type "lvalue-reference to cv12 S." If the template-argument names a type "rvalue-reference to cv1 S," an attempt to create the type "rvalue-reference to cv2 T" creates the type "rvalue-reference to cv12 S." Redundant cv-qualifiers are ignored.
这里是2118,其中引用了引号:
Here is 2118 where the quote has been struck out:
http:// www .open-std.org / jtc1 / sc22 / wg21 / docs / papers / 2006 / n2118.html
14.3。 1 - 模板类型参数
14.3.1 - Template type arguments
-4-如果模板参数T的模板参数命名类型cv1 S引用即对类型A的引用,尝试创建类型对cv2 T的引用对于cv T的引用创建类型对cv12 S的引用,其中cv12是cv限定符cv1和cv2的联合。忽略冗余cv限定符lvalue-reference to A,而尝试创建类型rvalue-reference to cv T则创建类型T.
-4- If a template-argument for a template-parameter T names a type "reference to cv1 S" that is a reference to a type A, an attempt to create the type "reference to cv2 T" "lvalue-reference to cv T" creates the type "reference to cv12 S", where cv12 is the union of the cv-qualifiers cv1 and cv2. Redundant cv-qualifiers are ignored "lvalue-reference to A", while an attempt to create the type "rvalue-reference to cv T" creates the type T.
您所引用的内容似乎是过时的措辞。
What you are quoting seems to be obsolete wording.
这篇关于C ++ 98/03参考崩溃和cv限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!