use*_*538 9 c++ decltype auto type-deduction c++17
#include <type_traits>
int main()
{
int arr[1] = { 6 };
auto& ref1 = arr[0];
static_assert( std::is_same_v<decltype( ref1 ), int&> ); //ok
auto& [ ref2 ] = arr;
static_assert( std::is_same_v<decltype( ref2 ), int> ); //ok
static_assert( std::is_same_v<decltype( ref2 ), int&> ); //error
}
Run Code Online (Sandbox Code Playgroud)
标识符ref1与ref2该示例之间的重要区别是什么?据我所知,ref2在结构绑定中也有一个引用类型,但为什么要为它decltype指示一个非引用类型?
decltype(e)根据e作为参数给出的内容,行为会有所不同.对于结构化绑定,decltype产生以下内容,[dcl.type.simple]:
对于表达式
e,表示的类型decltype(e)定义如下:
- if
e是未命名的id-expression,命名结构化绑定,decltype(e)是结构化绑定声明规范中给出的引用类型
具有数组类型表达式作为初始化程序的结构化绑定声明的引用类型是元素[dcl.struct.bind]的类型:
如果
E是具有元素类型的数组类型,T则identifier-list中的元素数应等于元素的数量E.每个v i 是一个左值的名称,它引用数组的元素i,其类型是T; 引用的类型是T.[ 注意:T的顶级cv限定符是cv.- 结束说明 ]
| 归档时间: |
|
| 查看次数: |
265 次 |
| 最近记录: |