简体   繁体   English

运营商演员,GCC和clang:哪个编译器是对的?

[英]Operator cast, GCC and clang: which compiler is right?

Consider the following code: 请考虑以下代码:

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();
}

It compiles with GCC (4.9/5.1/6.1), but it fails to compile with clang (3.8/3.7). 它使用GCC(4.9 / 5.1 / 6.1)进行编译,但无法使用clang(3.8 / 3.7)进行编译。
The error returned is: 返回的错误是:

error: unknown type name 'T'; 错误:未知类型名称'T'; did you mean 'S::T'? 你是说'S :: T'吗?

Which compiler is right in this case and why? 在这种情况下哪个编译器是正确的,为什么?

Note 注意

Solving it is a matter of qualifying T : 解决它是一个限定T

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

The question is not about how to make it work. 问题不在于如何使其发挥作用。

I believe this is clang bug (submitted as #27807 ) 我相信这是clang bug(提交为#27807

From [basic.lookup.classref] : 来自[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. 如果id-expressionconversion-function-id ,则首先在对象表达式的类中查找其conversion-type-id ,并使用名称(如果找到)。 Otherwise it is looked up in the context of the entire postfix-expression . 否则,它将在整个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 ] - 末端的例子]

In t = s.operator T(); t = s.operator T(); , T is first looked up in the class of S , which should find your typedef and hence end up calling operator int() . T首先在S类中查找,它应该找到你的typedef,因此最终调用operator int()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Clang vs GCC vs MSVC模板转换运算符 - 哪个编译器是对的? - Clang vs GCC vs MSVC template conversion operator - which compiler is right? 在类嵌套的静态const成员变量初始化Clang vs GCC哪个编译器是对的? - In class nested static const member variable initialization Clang vs GCC which compiler is right? C ++函数到指针的隐式转换:哪个编译器正确? 铛和海湾合作委员会不同意 - C++ function-to-pointer implicit conversion: which compiler is right? Clang and GCC disagree 在Clang上重载操作符歧义但在GCC上没有重载,哪一个是正确的? - Overloaded operator ambiguity on Clang but not on GCC, which one is correct? 转换运算符:gcc vs clang - Conversion operator: gcc vs clang operator== 使用 msvc 编译,但不使用 gcc 和 clang - operator== compiles with msvc but not with gcc and clang 重载 operator delete 而不重载 new 时的正确行为是什么? gcc 和 clang 不同 - What's the right behavior when overloading operator delete without overloading new? gcc and clang differ 将默认赋值运算符声明为constexpr:哪个编译器是对的? - Declaring defaulted assignment operator as constexpr: which compiler is right? 嵌套私有枚举的Operator ++-哪个编译器正确? - Operator++ for nested private enum - which compiler is right? clang 和 gcc 以不同的方式解释系列演员表 - clang & gcc interpret series of cast differently
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM