【发布时间】:2021-09-18 07:34:42
【问题描述】:
std::common_reference 将decltype 用于三元运算符?:
否则,如果
decltype(false? val<T1>() : val<T2>())(其中 val 是函数模板template<class T> T val();)是有效类型,则成员类型类型命名该类型;
但是 MSVC 在下面的 C++20 代码中说 decltype(false? val<int&&>() : val<int&&>()) 是 int。
#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 缺陷吗?
【问题讨论】:
-
行为应该是在这里返回一个右值引用。 cppreference 用“
(4) If E2 and E3 are glvalues of the same type and the same value category, then the result has the same type and value category, and is a bit-field if at least one of E2 and E3 is a bit-field.”证实了这一点。 cppreference 当然不是一个规范的参考,但仍然是一个很好的健全性检查。 MSVC 可能会保留这种不良行为以防止破坏以前编译的代码。
标签: c++ visual-studio c++20