#include <iostream>
#include <initializer_list>
using namespace std;
struct CL
{
CL(){}
CL (std::initializer_list<CL>){cout<<1;}
CL (const CL&){cout<<2;}
};
int main()
{
CL cl1;
CL cl2 {cl1}; //prints 21
}
Here is CL struct with copy constructor and initializer-list constructor. I think only copy constructor must be called here, because according to C++ 14 Standard, 8.5.4/3
List-initialization of an object or reference of type T is defined as follows:
— If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization).
— Otherwise, ...
In other words, the initialization of cl2 must be performed from cl1 element, but not from the initializer-list {cl1}. Clang and gcc both print "21", only Visual Studio prints "2" and I think it's correct.
There are two candidate constructors for taking an argument cl1 of type CL:
- Constructor with
std::initializer_list<CL>
(passes because no such conversion from CL tostd::initializer_list<CL>
) - Copy constructor with const CL& (exact match with only qualification conversion non-const->const)
Who is right? Whose behavior is correct?
2
as expected. – juanchopanza21
but clang 3.7 says2
; all vers of g++ up to 5.2 say21
– M.M