Coc*_*coa 5 c++ decltype c++14
int main(){\n decltype(auto)&& a = 100;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n上面的代码,在GCC和Clang中出错。
\n\nint main(){\n decltype(int)&& a = 100;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这段代码正确。
\n\n在N4296中,
\n\n在\xc2\xa7 8.3.2/6
\n\n\n\n\n如果 typedef (7.1.3)、类型模板参数 (14.3.1) 或 decltype-specifier (7.1.6.2) 表示类型 TR\n 是对类型 T 的引用,则尝试创建type \xe2\x80\x9clvalue 对 cv TR\xe2\x80\x9d 的引用创建类型\n \xe2\x80\x9clvalue 对 T\xe2\x80\x9d 的引用,而尝试创建类型 \xe2\x80\ x9crvalue 对 cv TR\xe2\x80\x9d 的引用创建类型 TR。
\n
\xc2\xa7 中的decltype 说明符7.1.6.2
\n\n\n\n\ndecltype 说明符:
\n
\n \xe3\x80\x80\xe3\x80\x80decltype ( 表达式 )
\n \xe3\x80\x80\xe3\x80\x80decltype ( auto )
我认为\xc2\xa7 8.3.2/6的写法有问题。
\n\n为什么不允许引用decltype(auto) 。\n请告诉我相关标准的措辞。\n抱歉,英语不好。\n谢谢。
\n在 \xc2\xa7 7.1.6.4 [dcl.spec.auto]
\n\n\n\n\n如果占位符是 decltype(auto) 类型说明符,则声明的变量类型或函数的返回类型应单独为占位符。为变量或返回类型推导的类型按照 7.1.6.2 中的描述确定,就好像初始值设定项是 decltype 的操作数一样。
\n
所以这是允许的:
\n\ndecltype(auto) a = 100;\nRun Code Online (Sandbox Code Playgroud)\n\n但不是这个:
\n\ndecltype(auto)& a = 100;\nRun Code Online (Sandbox Code Playgroud)\n\n或这个 :
\n\ndecltype(auto)&& a = 100;\nRun Code Online (Sandbox Code Playgroud)\n\n这是有道理的,因为背后的想法之一decltype(auto)是在类型推导期间保留引用性(即使用decltype类型推导,而不是模板/自动类型推导)
该标准为我们提供了如何通过以下方式推导引用的示例decltype(auto):
int i;\nint&& f();\nauto x3a = i; // decltype(x3a) is int\ndecltype(auto) x3d = i; // decltype(x3d) is int\nauto x4a = (i); // decltype(x4a) is int\ndecltype(auto) x4d = (i); // decltype(x4d) is int&\nauto x5a = f(); // decltype(x5a) is int\ndecltype(auto) x5d = f(); // decltype(x5d) is int&&\nauto x6a = { 1, 2 }; // decltype(x6a) is std::initializer_list<int>\ndecltype(auto) x6d = { 1, 2 }; // error, { 1, 2 } is not an expression\nauto *x7a = &i; // decltype(x7a) is int*\ndecltype(auto)*x7d = &i; // error, declared type is not plain decltype(auto)\nRun Code Online (Sandbox Code Playgroud)\n