gpt4 book ai didi

c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:39 26 4
gpt4 key购买 nike

// 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;
}

编译结果如下:

错误:聚合 ‘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 非类类型的纯右值,如 C++17 [expr] 中所述:

If a prvalue initially has the type cv T, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

您的输出可能只是诊断消息中的错误。要确认编译器错误,您可以编写一些代码,其行为因 decltype 结果而异,例如:

decltype((const int)1) x;  x = 5;

应该编译成功。

关于c++ - 不同类型的 `decltype((const int)a)` 和 `decltype((const int)1)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50559090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com