I'm learning C++ and I got to the point where I'm reading about list initialization. After a bit of confusion,due to syntax overlapping, I understood that uniform initialization and list initialization are indeed two different (albeit as I just said overlapping) new features of C++11. So,before I ask my question I would like to recap,and please correct me if I'm geting something wrong (before that something becomes a misconception about this topic..)
as far as built-in types are concerned,the syntax for initializing them is as follows (I'm assuming int for readability):
// defines a default initialized int
int a;
// defines a value initialized int (binary zeroes)
int a{};
// equivalent definitions,define an int variable of value 10:
int a=10;
int a(10);
int a{10};
int a={10};
// equivalent definitions,define a temporary int variable of value 10:
int(10);
int{10};
// equivalent definitions,define a temporary int variable that is value initialized to 0:
int();
int{};
now to the interesting part, using the uniform initialization for aggregate types (arrays and aggregate structs). Until C++11 I could initialize arays and aggregate classes with a list initializer:
int array[]={1,2,3};
struct aggregate {
int a;
double b;
};
aggregate a={1,2.3};
now,I know that the new standard allows me to use the new syntax for the uniform initialization:
int array[]{1,2,3};
aggregate a{1,2,3};
this may come in handy for using in constructor's member initialization lists.
and now,to the class types. If a class doesn't define a initializers_list constructor:
// equivalent definitions,define a class_type variable,default initialized:
class_type name;
class_type name{};
// equivalent definitions,define a class_type variable,using the appropriate constructor:
class_type name(*args*);
class_type name{*args*};
class_type name={*args*};
// defines a class_type variable and calls copy-constructor (allows implicit conversion to class_type if converting constructor is non-explicit)
class_type name=name2;
// equivalent definitions,define a temporary class_type object,default initialized
class_type();
class_type{};
// equivalent definitions,define a temporary class_type object,initialized by the appropriate constructor
class_type(*args*);
class_type{*args*};
I know that when a class defines a class_type::class_type(const std::initializer_list&) constructor, it takes precedence over other constructors (unless it's an empty list and then the default constructor takes precedence).
Now, my question is:
what does the =
change when I use it with a braced list? The only difference I could think of is whether the init list constructor is explicit or not.Is
class_type name{*args*};
class_type name={*args*};
the same as
class_type name({*args*});
class_type name=class_type(std::initializer_list({*args*}));
and so copy initialization implicit conversion are involved? is it the only difference?
please, correct me if I'm saying something blaringly wrong!