问题描述:

The following program prints 42:

#include <iostream>

struct A

{

operator int(){ return 42; }

};

struct B

{

operator A(){ return A(); }

};

B b;

int a = A(b);

int main(){ std::cout << a << std::endl; } //42

DEMO

But if we try to define cope/move or both contructors it won't work.

#include <iostream>

struct A

{

A(A&&){ std::cout << "A(A&&)" << std::endl; }

A(A&){ std::cout << "A(A&)" << std::endl; }

operator int(){ return 42; }

};

struct B

{

operator A(){ return A(); }

};

B b;

int a = A(b);

int main(){ std::cout << a << std::endl; } //Error

DEMO

I thought, the relevant section, describing that behavior is N4296::8.5/17.7 [dcl.init]

If the destination type is a (possibly cv-qualified) class type:

[...]

— Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered. The applicable conversion

functions are enumerated (13.3.1.5), and the best one is chosen

through overload resolution (13.3). The user-defined conversion so

selected is called to convert the initializer expression into the

object being initialized. If the conversion cannot be done or is

ambiguous, the initialization is ill-formed.

It shouldn't depends on absence/presence of the constructors. We just should have to have appropriate conversion functions so as to choose the conversion sequence.

网友答案:

You effectively deleted the default constructor. From the standard (12.1/4, emphasis mine):

A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted

IF there is no user-declared constructor. But you declared two, so there is no implicit default constructor. Thus, this:

operator A(){ return A(); }
                  // ^^^

can't compile. That's why the error you get is

error: no matching function for call to A::A()

The code tries to call your conversion operator - but the body isn't valid.

相关阅读:
Top