oli*_*ist 5 c++ type-conversion decltype
// g++ 7.3
template<typename T>
struct td;
int main()
{
int a = 1;
td<decltype((const int)a)> t1;
td<decltype((const int)1)> t2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
下面是编译的输出:
错误:聚合‘td<int> t1’类型不完整且无法定义
错误:聚合‘td<const int> t2’类型不完整且无法定义
那么,为什么类型decltype((const int)a)和decltype((const int)1)不同?
说明者decltype((const int)a)和decltype((const int)1)两者都决心int.这是因为没有const非类型的prvalues,如C++ 17 [expr]中所述:
如果prvalue最初具有类型
cv T,其中Tcv不合格的非类非数组类型,则T在进行任何进一步分析之前调整表达式的类型.
您的输出可能只是诊断消息中的错误.要确认编译器错误,您可以编写一些代码,其行为因decltype结果而异,例如:
decltype((const int)1) x; x = 5;
Run Code Online (Sandbox Code Playgroud)
哪个应该编译成功.