简体   繁体   English

在隐式类型转换期间,最多可隐式应用多少用户定义的转换运算符?

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

相关问题 用户定义类型的隐式类型转换 - Implicit type conversion for user-defined type 隐式转换为用户定义的类型 - Implicit Conversion to a user-defined type 使用用户定义的转换运算符隐式转换 - Converting this implicitly using an user-defined conversion operator 模板推导与隐式用户定义转换运算符 - Template deduction vs. implicit user-defined conversion operator c ++用于模板类的用户定义运算符的隐式转换 - c++ implicit conversion on user-defined operator for template classes 为什么在初始化期间应用了用户定义的转换? - Why does user-defined conversion applied during the initialization? 调用重载运算符时,用户定义的枚举类的隐式转换失败 - User-defined implicit conversion of an enum class when calling an overloaded operator fails 调用 operator== 时,MSVC 不会执行用户定义的到 std::nullptr_t 的隐式转换 - MSVC won't perform a user-defined implicit conversion to std::nullptr_t when invoking operator== 为什么没有在调用对象上隐式进行用户定义的转换 - Why user-defined conversion is not implicitly taking place on the calling object C++中从用户定义类型到基本类型的隐式转换 - Implicit conversion from user-defined type to primitive type in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM