[英]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-expression是conversion-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.