为什么不能形成对 'decltype(auto)' 的引用
Why cannot form reference to 'decltype(auto)'
int main(){
decltype(auto)&& a = 100;
}
以上代码,在GCC和Clang中出错
int main(){
decltype(int)&& a = 100;
}
此代码正确。
在N4296中,
在§ 8.3.2/6
If a typedef (7.1.3), a type template-parameter (14.3.1), or a decltype-specifier (7.1.6.2) 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.
decltype-specifier § 7.1.6.2
decltype-specifier:
decltype ( expression )
decltype ( auto )
我认为§8.3.2/6的措辞有问题。
为什么不允许引用 decltype(auto)。
请告诉我相关标准的措辞。
抱歉英语不好。
谢谢。
在 § 7.1.6.4 [dcl.spec.auto]
If the placeholder is the decltype(auto) type-specifier, the declared
type of the variable or return type of the function shall be the
placeholder alone. The type deduced for the variable or return type is
determined as described in 7.1.6.2, as though the initializer had been
the operand of the decltype.
所以这是允许的:
decltype(auto) a = 100;
但不是这个:
decltype(auto)& a = 100;
或者这个:
decltype(auto)&& a = 100;
这是有道理的,因为 decltype(auto)
背后的想法之一是在类型推导期间保留引用性(即使用 decltype
类型推导,而不是 template/auto 类型推导)
该标准为我们提供了如何通过 decltype(auto)
推导引用的示例:
int i;
int&& f();
auto x3a = i; // decltype(x3a) is int
decltype(auto) x3d = i; // decltype(x3d) is int
auto x4a = (i); // decltype(x4a) is int
decltype(auto) x4d = (i); // decltype(x4d) is int&
auto x5a = f(); // decltype(x5a) is int
decltype(auto) x5d = f(); // decltype(x5d) is int&&
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
auto *x7a = &i; // decltype(x7a) is int*
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)
int main(){
decltype(auto)&& a = 100;
}
以上代码,在GCC和Clang中出错
int main(){
decltype(int)&& a = 100;
}
此代码正确。
在N4296中,
在§ 8.3.2/6
If a typedef (7.1.3), a type template-parameter (14.3.1), or a decltype-specifier (7.1.6.2) 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.
decltype-specifier § 7.1.6.2
decltype-specifier:
decltype ( expression )
decltype ( auto )
我认为§8.3.2/6的措辞有问题。
为什么不允许引用 decltype(auto)。 请告诉我相关标准的措辞。 抱歉英语不好。 谢谢。
在 § 7.1.6.4 [dcl.spec.auto]
If the placeholder is the decltype(auto) type-specifier, the declared type of the variable or return type of the function shall be the placeholder alone. The type deduced for the variable or return type is determined as described in 7.1.6.2, as though the initializer had been the operand of the decltype.
所以这是允许的:
decltype(auto) a = 100;
但不是这个:
decltype(auto)& a = 100;
或者这个:
decltype(auto)&& a = 100;
这是有道理的,因为 decltype(auto)
背后的想法之一是在类型推导期间保留引用性(即使用 decltype
类型推导,而不是 template/auto 类型推导)
该标准为我们提供了如何通过 decltype(auto)
推导引用的示例:
int i;
int&& f();
auto x3a = i; // decltype(x3a) is int
decltype(auto) x3d = i; // decltype(x3d) is int
auto x4a = (i); // decltype(x4a) is int
decltype(auto) x4d = (i); // decltype(x4d) is int&
auto x5a = f(); // decltype(x5a) is int
decltype(auto) x5d = f(); // decltype(x5d) is int&&
auto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>
decltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression
auto *x7a = &i; // decltype(x7a) is int*
decltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)