最少代码:
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, t
是 comma 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,
otherwise, if
e
is an lvalue,decltype(e)
isT&
, whereT
is the type ofe
;
顺便说一句:我尝试使用 MSVC here使用 Gcc 和 Clang 得到了相同的结果。
关于c++ - decltype(1, t) 应该是左值引用吗? (编译器不同意),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60272602/