[英]At most how many user-defined conversion operator can be implicitly applied during an implicit type conversion?
According to the working draft N3337 (the most similar draft to the published ISOC++11 standard) the answer is at most one. 根据N3337工作草案(与已发布的ISOC ++ 11标准最相似的草案),答案最多只有一个。
N3337 : N3337 :
At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value.
最多一个用户定义的转换(构造函数或转换函数)隐式应用于单个值。
[ Example: struct X { operator int(); }; struct Y { operator X(); }; Y a; int b = a; // error // a.operator X().operator int() not tried int c = X(a); // OK: a.operator X().operator int() —end example ]
But according to the result of compiling main.cpp with gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 and running a.out with the quoted statements in Ubuntu 14.04.3 LTS , the answer is not at most one. 但是根据使用gcc(Ubuntu 4.8.4-2ubuntu1~14.04)4.8.4编译main.cpp并在Ubuntu 14.04.3 LTS中使用引用语句运行a.out的结果 ,答案最多不是一个。
main.cpp : main.cpp :
#include <iostream>
struct As
{
operator int(){ std::cout<<"operator As::int()"<<std::endl; return 1; }
};
struct Bs
{
operator int(){ std::cout<<"operator Bs::int()"<<std::endl; return As(); }
};
int main()
{
int i=Bs();
return 0;
}
compiling and running from terminal: 从终端编译和运行:
$ g++ -std=c++11 main.cpp
$ ./a.out
the result (output): 结果(输出):
operator Bs::int()
operator As::int()
Did I misunderstand something or is N3337 wrong or does gcc contains a bug? 我误解了某些内容或者N3337是否错误或者gcc是否包含错误?
There are no double conversions getting executed here. 这里没有双重转换执行。
You have two individual conversions taking place, in two separate places. 您可以在两个不同的位置进行两次单独转换。
One conversion is in B::operator int()
. 一个转换在
B::operator int()
。
The second conversion is in your main()
. 第二次转换是在你的
main()
。
Let's try to think through this logically: 让我们试着从逻辑上思考:
Remove main() entirely from your translation unit. 完全从翻译单元中删除main()。 Do you see any double conversions?
你看到任何双重转换吗?
No. 没有。
Now, let's create a header file containing the following bits, call it structures.H
: 现在,让我们创建一个包含以下位的头文件,称之为
structures.H
:
struct As
{
operator int();
};
struct Bs
{
operator int();
};
Now, create a structures.C
file, containing the contents of each one of these operators: 现在,创建一个
structures.C
文件,其中包含以下每个运算符的内容:
#include <structures.H>
B::operator int(){ std::cout<<"operator As::int()"<<std::endl; return 1; }
A::operator int(){ std::cout<<"operator Bs::int()"<<std::endl; return As(); }
Ok, do you still see any double-conversions here? 好的,你还能在这里看到任何双重转换吗? No.
没有。
Now, create your main.C: 现在,创建你的main.C:
#include <structures.H>
int main()
{
int i=Bs();
return 0;
}
Do you see any double conversions taking place here? 你看到在这里发生了双重转换吗? No, even though what we have now, with the two translation units, the same exact code you started with.
不,即使我们现在拥有两个翻译单元,您开始使用相同的确切代码。
int i=Bs();
invokes Bs::operator int()
implicitly. 隐式调用
Bs::operator int()
。
return As()
invokes As::operator int()
implicitly. return As()
As::operator int()
隐式调用As::operator int()
。
These are two separate expressions. 这是两个单独的表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.