gpt4 book ai didi

c++ - 运算符转换、GCC 和 clang : which compiler is right?

转载 作者:IT老高 更新时间:2023-10-28 22:28:20 25 4
gpt4 key购买 nike

考虑以下代码:

struct S {
using T = int;
operator T() { return 42; }
};

int main() {
S s;
S::T t = s;
// Is the following line correct?
t = s.operator T();
}

使用 GCC (4.9/5.1/6.1) 编译,但使用 clang (3.8/3.7) 编译失败。
返回的错误是:

error: unknown type name 'T'; did you mean 'S::T'?

在这种情况下哪个编译器是正确的,为什么?

注意

解决它是一个合格的问题 T:

t = s.operator S::T();

问题不在于如何让它发挥作用。

最佳答案

我认为这是 clang 错误(以 #27807 提交)

来自 [basic.lookup.classref] :

If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression. In each of these lookups, only names that denote types or templates whose specializations are types are considered. [ Example:

struct A { };
namespace N {
struct A {
void g() { }
template <class T> operator T();
};
}

int main() {
N::A a;
a.operator A(); // calls N::A::operator N::A
}

—end example ]

t = s.operator T(); 中,首先在 S 的类中查找 T,它应该会找到你的typedef 并因此最终调用 operator int()

关于c++ - 运算符转换、GCC 和 clang : which compiler is right?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37310866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com