c++ - 带有双括号的 decltype((x)) 是什么意思?

标签 c++ c++11 reference rvalue-reference decltype

<分区>

非常简单的问题,我无法用谷歌搜索出答案。

例如:

int a = 0;
int& b = x;
int&& c = 1;

decltype((a)) x; // what is the type of x?
decltype((b)) y; // what is the type of y?
decltype((c)) z; // what is the type of z?

也许我应该为 x、y 和 z 分配一些值以获得不同的结果,我不确定。

编辑: 根据下面的站点,双括号将示例 int 转换为引用: https://github.com/AnthonyCalandra/modern-cpp-features#decltype

int a = 1; // `a` is declared as type `int`
int&& f = 1; // `f` is declared as type `int&&`
decltype(f) g = 1; // `decltype(f) is `int&&`
decltype((a)) h = g; // `decltype((a))` is int&

最佳答案

它们都是int&类型。

(a)一样添加括号使它们成为表达式(而不是实体),它们都是左值(作为命名变量);然后 decltype产生 T&,即此处的 int&

...

4) If the argument is any other expression of type T, and

...

b) if the value category of expression is lvalue, then decltype yields T&;

...

您可以使用这个 LIVE DEMO 检查实际类型(来自编译错误消息)。

关于c++ - 带有双括号的 decltype((x)) 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56541936/

相关文章:

c++ - 读取文件到 istream 替换类变量

c++ - 升级到1.2.162.1后 : vkQueueWaitIdle == VK_ERROR_DEVICE_LOST

c++ - 创建可点击的 "buttons"C++

c++ - 使用 SFINAE 检查类型是否可以绑定(bind)到模板模板参数

c++ - 如何将 unique_ptr.get() 的引用传递给函数

c++ - C++与QML之间的通信

c++ - 使用 reinterpret_cast 将数据从 std::vector<double> reshape 为指定维度的 double**

multithreading - 使用 OMP 循环展开

c++ - 引用指针错误 : Non-const lvalue reference "const * FooBarClass" cannot bind to a temporary

sql - 检查行是否已经存在,如果存在则告诉引用的表 id