为什么触发此 noexcept 运算符 MSVC 的“重载具有类似的转换”错误,而不是 Clang 和 GCC

问题描述

我有以下代码

struct S {
    template<typename T>
    operator T() const noexcept
    (noexcept(static_cast<T>(1) - 1)) // comment out this line to make it work in MSVC
    {
        return static_cast<T>(1) - 1;
    }
};
struct R {
    R(int i) {}
    operator int() const { return 0; }
    int operator-(R const& o) const { return 0; }
};

bool operator==(R const& l,int r) {
    return true;
}
bool operator==(S const& l,int r) {
    return true;
}

int main() {
    return S() == 3;
}

在 C++11 及更高版本的 Clang 和 GCC 上编译良好,但抱怨

'R::operator -': 2 overloads have similar conversions
note: Could be 'int R::operator -(const R &) const'
note: or       'built-in C++ operator-(int,int)'
note: while trying to match the argument list '(T,int)' with [ T=R ]

在 MSVC 中。

令我困惑的是,删除 template operator T() 上的 struct S 上的 noexcept operator 会使错误消失。它的表达式与它标记noexcept方法主体中的表达式完全相同,我希望方法主体会导致相同的错误,但不会。

为什么表达式 static_cast<T>(1) - 1 在未计算的上下文中编译时会导致编译器错误,而在方法体中编译时却不会?

在这里,为什么?

这是一个 godbolt

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...