initialization list with conversion operator dont work properly and report error

for the following code:

struct X
{
X();
};

struct Y
{
operator X() const;
};

X a = { Y() }; // reports error: no matching constructor for initialization of ‘X’
X aa = Y(); // works fine

clang when compiled with std=c++11 gives compilation errors as:

testfile.C:11:3: error: no matching constructor for initialization of ‘X’
X a = { Y() }; // reports error: no matching constructor for initialization of ‘X’
^ ~~~~~~~
testfile.C:1:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from ‘Y’ to ‘const X &’ for 1st
argument
struct X
^
testfile.C:1:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from ‘Y’ to ‘X &&’ for 1st
argument
struct X
^
testfile.C:3:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
X();
^
1 error generated.

See C++11 [over.best.ics]p4. We could probably improve the diagnostic
here, though; please file a bug.

-Eli

Actually it should have not thrown error at all. it works fine with gcc. And the part of code which you mentioned is not getting hit at all. Maybe some difference in parsing is there.

I’m not really an overload resolution expert, so I could be wrong. Anyway, please file a bug report (http://llvm.org/bugs/), and our overload resolution experts will take a look. :slight_smile:

-Eli

what i think could be the possible error is that while evaluating our expression X a = {Y()}; , in the function InitializationSequence we call the function which in turn calls TryListInitialization function, which evaluates all the possible contructors for overloading, but does not check for conversion functions. so TryListInitialization reports failure in finding any candidate function for overloading.

whereas in the code flow for X aa = Y(); the the function TryUserDefinedConversion is called from InitializationSequence which is able to properly get the candidate function.

So maybe we should call TryUserDefinedConversion from within the TryListInitialization function passing Y() of the initlist in the cases when normal overloading is failing.

Please suggest.