简体   繁体   English

定义转换构造函数和转换运算符时哪个优先,为什么编译器不同这种转换?

[英]Which gets precedence when Converting Constructor as well as Conversion Operator are defined and why compilers differ this conversion?

What does standard say about the precedence of Converting Constructor and Conversion Operator , in simple language?标准在简单语言中对Converting ConstructorConversion Operator的优先级有何看法?

Also, I can see that when I have 2 classes myClass and otherClass and I want to convert otherClass to myClass as shown below, then Conversion operator gets called for class otherClass另外,我可以看到,当我有 2 个类myClassotherClass并且我想将otherClass转换为myClass ,如下所示,然后转换运算符被调用类otherClass

myClass mc = otherclass();//creates temp for otherClass

This behavior is same in gcc as well as MSVC.这种行为在 gcc 和 MSVC 中是相同的。

But this behavior is different across above compilers when I do something like below-但是当我执行以下操作时,上述编译器的这种行为是不同的 -

otherClass oC;
myClass mc = oC;//for gcc (C++11): Ambiguous, for MSVC: calls myClass Conversion Constructor

So, my question is why initializing from temporary in 1st case is different than the 2nd case?所以,我的问题是为什么在第一种情况下从临时初始化与第二种情况不同? And secondly why compilers differ behavior for 2nd case?其次,为什么编译器对第二种情况的行为不同?

EDIT: Classes defined as编辑:类定义为

#include <iostream>
using namespace std;

class otherClass;
class myClass {
      public:
          myClass(){}
          myClass(otherClass&) {
          cout << "called myClass's conversion constructor" << endl;
          }
};

class otherClass {
      public:
         operator myClass () {
             cout << "called otherClass's conversion operator" << endl;
             return myClass ();
         }
};

int main()
{
    otherClass oc;
    myClass mc = oc;
    myClass mc1 = otherClass();
    return 0;
}

Which gets precedence when Converting Constructor as well as Conversion Operator are defined定义转换构造函数和转换运算符时哪个优先

Neither.两者都不。

why compilers differ this conversion?为什么编译器不同这种转换?

If the conversion sequence is ambiguous, then the program is ill formed.如果转换顺序不明确,则程序格式错误。 Thus the compiler is allowed to not produce a program, and required to diagnose the issue.因此,允许编译器不生成程序,并需要诊断问题。 Allowing such ambiguous conversion intentionally would be considered a language extension, but failing to diagnose regardless of the extension would be a failure to conform to the standard.故意允许这种模棱两可的转换将被视为语言扩展,但无论扩展如何都无法诊断将是不符合标准。

If the conversion sequence is unambiguous, and there is no other reason for the program to be ill formed, then a compiler that refuses to compile it does not conform to the standard.如果转换顺序是明确的,并且没有其他原因导致程序格式错误,那么拒绝编译它的编译器不符合标准。


Edit: Regarding the added example: The conversion of myClass mc = oc is indeed ambiguous and the program is ill-formed.编辑:关于添加的示例: myClass mc = oc的转换确实不明确,并且程序myClass mc = oc So, a possible reason for difference in behaviour is either a language extension of the compiler that allows it, or a compiler bug.因此,行为差异的可能原因是允许它的编译器的语言扩展,或者编译器错误。 If it is not diagnosed, then the compiler does not conform to the standard.如果不诊断,则编译器不符合标准。 I recommend disabling language extensions.我建议禁用语言扩展。

myClass mc1 = otherClass() is well-formed because there is only one valid candidate for the conversion. myClass mc1 = otherClass()是格式良好的,因为只有一个有效的转换候选者。 The converting constructor is not a valid candidate because lvalue references to non-const cannot be bound to rvalues.转换构造函数不是有效的候选者,因为对非常量的左值引用不能绑定到右值。

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

相关问题 C ++转换运算符与构造函数优先级,编译器有所不同 - C++ conversion operator vs constructor precedence, compilers differ 转换运算符重载歧义,编译器不同 - conversion operator overloading ambiguity, compilers differ 转换构造函数与转换运算符:优先级 - Conversion constructor vs. conversion operator: precedence 为什么转换构造函数比转换运算符更受欢迎? - Why is the converting constructor preferred to the conversion operator? (涉及显式)优先级与运算符和构造函数转换 - (involving explicit)Precedence with operator and constructor conversion 类型转换运算符未用于强制转换,编译器有所不同 - type conversion operator not being used to cast, compilers differ 隐式用户定义的转换不起作用,因为在编译C ++时无法识别运算符和转换构造函数 - Implicit user defined conversion not working because operator and conversion constructor are unrecognized when compiling C++ 这是构造函数运算符还是转换运算符? - Is this a constructor operator or conversion operator? 为什么在没有赋值运算符的情况下调用转换构造函数? - Why is conversion constructor called in absense of assignment operator? 为什么转换可以涉及两个用户定义的转换函数/构造函数? - Why conversion can involve two user defined conversion function/constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM