For example I guess I understand what list-initialization in context of direct-initialization (vs copy-) mean - int x{}
vs int x = {}
basically.
But on cppreference I found this:
When an object of class type is copy-initialized from an object of the same or derived class type, or default-initialized in a copy-initialization context, the candidate functions are all converting constructors of the class being initialized. The argument list is the expression of the initializer.
I guess I understand why candidates are converting constructors for the first case, but not for the second. I mean, I can't write something like MyClass x = MyClass
, and = MyClass()
would be a value-initialization, and = MyClass(args...)
would be a direct-initilization.
And even if such a construct existed, I don't see why a temporary MyClass
object 'construction' should include specifically all converting constructors.
(And x
is not something that's talked about here as I see, because it's definitely copy-constructed, not default-constructed.)
So I guess I'm confused with the terms here.
The Copy initialization is basically an overloaded constructor. Direct initialization can be done using assignment operator. This initializes the new object with an already existing object. This assigns the value of one object to another object both of which are already exists.
In other words, a good compiler will not create a copy for copy-initialization when it can be avoided; instead it will just call the constructor directly -- ie, just like for direct-initialization.
Default member initializer (C++11) [edit] This is the initialization performed when an object is constructed with no initializer.
Explicit initialization with constructors (C++ only) A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.
This wording is added in the paper P0398R0, which is intented to describe the following case:
Z c = {};
for non-aggregate Z
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With