模板中引用的引用类型是什么 class
What is the type of a reference of a reference in a template class
在下面的代码中,a 和 b 的类型是什么?
template <class T = const int&>
struct A
{
T& a;
T b;
};
int main() {
int i = 1;
A<> a{i, i};
return 1;
}
我使用了这个 post 中的代码,它可以给出变量的类型。 -> post
但是,它说两种类型都是 i const&
。
int main() {
int i = 1;
A<> a{i, i};
std::cout << type_name<decltype(a.a)>() << std::endl;
std::cout << type_name<decltype(a.b)>() << std::endl;
return 0;
}
上面的例子中T&
和T
是同一种类型吗?
这些 & 符号是否结合起来成为 r 值或其他规则?
T
是 const int&
因为那是你告诉它的。
T&
也是 const int&
因为 引用折叠 将你的 T&
转换为 T
:
[dcl.ref]/6:
If a typedef-name ([dcl.typedef], [temp.param]) or a decltype-specifier ([dcl.type.simple]) denotes a type TR
that is a reference to a type T
, an attempt to create the type “lvalue reference to cv TR
” creates the type “lvalue reference to T
”, while an attempt to create the type “rvalue reference to cv TR
” creates the type TR
. [ Note: This rule is known as reference collapsing. — end note ] [ Example:
int i;
typedef int& LRI;
typedef int&& RRI;
LRI& r1 = i; // r1 has the type int&
const LRI& r2 = i; // r2 has the type int&
const LRI&& r3 = i; // r3 has the type int&
RRI& r4 = i; // r4 has the type int&
RRI&& r5 = 5; // r5 has the type int&&
decltype(r2)& r6 = i; // r6 has the type int&
decltype(r2)&& r7 = i; // r7 has the type int&
— end example ]
这是为了方便,因为没有 const int& &
这样的东西(引用引用;不要与确实存在的右值引用类型 const int&&
混淆!)能够像您一样编写代码而无需手动 "get rid of" "extra" &
.
此规则背后的基本原理在此处有更详细的解释:
- Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&&
在下面的代码中,a 和 b 的类型是什么?
template <class T = const int&>
struct A
{
T& a;
T b;
};
int main() {
int i = 1;
A<> a{i, i};
return 1;
}
我使用了这个 post 中的代码,它可以给出变量的类型。 -> post
但是,它说两种类型都是 i const&
。
int main() {
int i = 1;
A<> a{i, i};
std::cout << type_name<decltype(a.a)>() << std::endl;
std::cout << type_name<decltype(a.b)>() << std::endl;
return 0;
}
上面的例子中T&
和T
是同一种类型吗?
这些 & 符号是否结合起来成为 r 值或其他规则?
T
是 const int&
因为那是你告诉它的。
T&
也是 const int&
因为 引用折叠 将你的 T&
转换为 T
:
[dcl.ref]/6:
If a typedef-name ([dcl.typedef], [temp.param]) or a decltype-specifier ([dcl.type.simple]) denotes a typeTR
that is a reference to a typeT
, an attempt to create the type “lvalue reference to cvTR
” creates the type “lvalue reference toT
”, while an attempt to create the type “rvalue reference to cvTR
” creates the typeTR
. [ Note: This rule is known as reference collapsing. — end note ] [ Example:int i; typedef int& LRI; typedef int&& RRI; LRI& r1 = i; // r1 has the type int& const LRI& r2 = i; // r2 has the type int& const LRI&& r3 = i; // r3 has the type int& RRI& r4 = i; // r4 has the type int& RRI&& r5 = 5; // r5 has the type int&& decltype(r2)& r6 = i; // r6 has the type int& decltype(r2)&& r7 = i; // r7 has the type int&
— end example ]
这是为了方便,因为没有 const int& &
这样的东西(引用引用;不要与确实存在的右值引用类型 const int&&
混淆!)能够像您一样编写代码而无需手动 "get rid of" "extra" &
.
此规则背后的基本原理在此处有更详细的解释:
- Concise explanation of reference collapsing rules requested: (1) A& & -> A& , (2) A& && -> A& , (3) A&& & -> A& , and (4) A&& && -> A&&