c++ - 三元运算符的 decltype 在 MSVC ~C++17 中不同

标签 c++ visual-studio c++20

std::common_reference 使用 decltype三元运算符 ?:

Otherwise, if decltype(false? val<T1>() : val<T2>()), where val is a function template template<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/

相关文章:

c++ - 具有嵌套结构的动态数组

c# - 有没有办法在调用变量名时使用字符串?

c++ - 使用 C++ 概念实现强类型的特征

c++ - 是否已经有一个 constexpr std::bit_cast 与 g++ 一起使用

c++ - 以编程方式创建的 tinyxml xml 文件未在 Internet Explorer 中加载

c++ - 将 Windows 主题应用于具有 WS_BORDER 样式的自定义控件

c# - 是否可以使用 VSPackage 添加新项目向导?

c++ - std::chrono::from_stream gcc 可用性,在 gcc 10.1.0 中不可用

c++ - 以PHP方式使用C++的Web开发环境

visual-studio - 为什么每个用户都存储 Visual Studio 项目的命令行设置?是否可以 checkin (和共享).user 设置文件?