问题描述
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
在未计算的上下文中编译时会导致编译器错误,而在方法体中编译时却不会?
谁在这里,为什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)