Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么不能引用';decltype(自动)和#x27;_C++_C++14_Decltype - Fatal编程技术网

C++ 为什么不能引用';decltype(自动)和#x27;

C++ 为什么不能引用';decltype(自动)和#x27;,c++,c++14,decltype,C++,C++14,Decltype,上面的代码,在GCC和Clang中有一个错误 int main(){ decltype(auto)&& a = 100; } 此代码是正确的 在N4296中 在§8.3.2/6中 如果类型定义(7.1.3)、类型模板参数(14.3.1)或decltype说明符(7.1.6.2)表示类型TR 这是对类型T的引用,尝试创建类型“lvalue reference to cv TR”将创建该类型 “对T的左值引用”,而尝试创建类型“对cv TR的右值引用”会创建类型TR §7.

上面的代码,在GCC和Clang中有一个错误

int main(){
    decltype(auto)&& a = 100;
}
此代码是正确的

在N4296中

在§8.3.2/6中

如果类型定义(7.1.3)、类型模板参数(14.3.1)或decltype说明符(7.1.6.2)表示类型TR 这是对类型T的引用,尝试创建类型“lvalue reference to cv TR”将创建该类型 “对T的左值引用”,而尝试创建类型“对cv TR的右值引用”会创建类型TR

§7.1.6.2中的decltype说明符

decltype说明符:
  decltype(表达式)
  decltype(自动)

我认为§8.3.2/6的措辞有问题

为什么不允许引用decltype(auto)。 请告诉我有关标准的措辞。 对不起,我的英语很差。
谢谢。

在§7.1.6.4[dcl.spec.auto]中

如果占位符是decltype(auto)类型说明符,则声明 变量类型或函数返回类型应为 仅占位符。为变量或返回类型推导的类型为 如7.1.6.2所述进行测定,如同初始值设定项 decltype的操作数

所以这是允许的:

int main(){
    decltype(int)&& a = 100;
}
但不是这个:

decltype(auto) a = 100;
或者这个:

decltype(auto)& a = 100;
这是有意义的,因为
decltype(auto)
背后的一个想法是在类型推断过程中保留引用性(即使用
decltype
类型推断,而不是模板/自动类型推断)


本标准为我们提供了如何通过
decltype(auto)
推导引用的示例:

inti;
int&&f();
自动x3a=i;//decltype(x3a)是int
decltype(自动)x3d=i;//decltype(x3d)是int
自动x4a=(i);//decltype(x4a)为int
decltype(自动)x4d=(i);//decltype(x4d)为int&
自动x5a=f();//decltype(x5a)为int
decltype(自动)x5d=f();//decltype(x5d)为int&&
自动x6a={1,2};//decltype(x6a)是std::initializer\u list
decltype(auto)x6d={1,2};//错误,{1,2}不是表达式
自动*x7a=&i;//decltype(x7a)为int*
decltype(自动)*x7d=&i;//错误,声明的类型不是纯decltype(自动)

谢谢。§8.3.2/6混淆。
decltype(auto)&& a = 100;
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)