std::common_reference
使用 decltype
三元运算符 ?:
Otherwise, if
decltype(false? val<T1>() : val<T2>())
, where val is a function templatetemplate<class T> T val();
, is a valid type, then the member type type names that type;
但是 MSVC 说 decltype(false? val<int&&>() : val<int&&>())
是int
在下面的 C++20 代码中。
#include <type_traits>
template<typename T>
T val();
int main() {
static_assert(std::is_same<int&&, decltype(val<int&&>())>::value, "1");
static_assert(std::is_same<int&&, decltype(false?val<int&&>():val<int&&>())>::value, "2");
}
https://godbolt.org/z/KE9PnGvd5
什么是正确的行为?
这是 MSVC 缺陷吗?
最佳答案
这是一个 MSVC 错误。
条件运算符 的规则在 [expr.cond] 中.这些规则中有很多部分非常复杂,但这种情况实际上很简单:
If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.
这就是我们的情况:我们有两个 int&&
,它们是相同值类别和相同类型的泛左值 - 所以结果也是 int&&
。
关于c++ - 三元运算符的 decltype 在 MSVC ~C++17 中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68291387/