【发布时间】:2021-10-06 08:45:24
【问题描述】:
从 C++20 开始,我们可以在 auto 关键字前面加上概念名称来限制可能的类型。特别是这种组合在类转换operator auto中是可能的,例如
template <typename T> concept x = true;
struct S
{
operator x auto() { return 2; }
operator auto() { return 1; }
};
int main() { return S{}.operator x auto(); }
但是 Clang 是唯一接受整个程序的编译器,但是 main() 返回 1(而不是我预期的 2),演示:https://gcc.godbolt.org/z/b16jYGa81
GCC 接受结构定义,但拒绝编译S{}.operator x auto()。
MSVC 甚至拒绝接受 struct S 并出现错误:
error C2535: 'S::operator auto(void)': member function already defined or declared
只是想知道,这里有哪些编译器(如果有的话)?
【问题讨论】:
-
显式调用运算符是一种不好的做法。转换运算符的目的是简化代码而不是使其更难阅读。
标签: c++ c++20 auto c++-concepts