c++ - decltype(1, t) 应该是左值引用吗? (编译器不同意)

标签 c++ templates c++17 sfinae decltype

最少代码:

int t;
static_assert(is_same_v<decltype(1, t), int&>);

上面的代码在 g++ 和 clang++ 中编译,但在 MSVC 中失败。 MSVC 似乎认为:

int t;
static_assert(is_same_v<decltype(1, t), int>);

标准规定了哪一项?对于 SFINAE,我非常依赖这种模式。

最佳答案

Gcc 和 Clang 是正确的。 1, tcomma expression ,

The type, value, and value category of the result of the comma expression are exactly the type, value, and value category of the second operand, E2.

第二个操作数,即t是左值,那么decltype将导致 T&

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

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

根据标准,[expr.comma]/1 :

(强调我的)

The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand,

[dcl.type.decltype]/1.5

otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;

顺便说一句:我尝试使用 MSVC here使用 Gcc 和 Clang 得到了相同的结果。

关于c++ - decltype(1, t) 应该是左值引用吗? (编译器不同意),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60272602/

相关文章:

c++ - 在 Mac OS 10.6 上使用 boost::xpressive 编译时间过长

C++模板类继承

c++ - 让 SFINAE 在重载函数对象上使用 `is_callable`

c++ - 是否对参与偏序的类型执行实例化

c++ - 数组内的自增运算符

c++ - connect() 上的参数 errno 无效

c++ - 什么时候使用 c++ iostreams 而不是 ReadFile、WriteFile、fprintf 等...?

templates - C++ for Rust 中特定模板用法的等价物

java - 从 Spark 到速度

c++ - 推导第一个模板参数与默认的其他模板参数