Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does direct initialization of class type consider user defined conversion function?

As I understand, clause 16.6.2 applies here for the direct initialization of a which considers only A‘s constructors.

But the code compiles fine.

Which part am I missing that causes the user defined conversion functions to also be considered?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

struct A {};
struct B {
    operator A();
};

void f()
{
    A a(B{});
}

>Solution :

A has an implicit move and an implicit copy constructor, both of which take references to A and the references in these parameters can be initialized by a call to the conversion operator in B.

In the end, overload resolution will choose the implicit move constructor.

That being said, this causes an unnecessary move construction on A which the compiler is not permitted to elide. The open CWG issue 2327 considers eliminating this unnecessary temporary object so that the conversion function can directly initialize the object and some compilers already handle it in some similar form (against the specification in the current standard).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading