Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'default-initialization in copy-initialization context' mean in C++?

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.

like image 308
ledonter Avatar asked May 07 '18 15:05

ledonter


People also ask

What is meant by copy initialization?

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.

Does copy initialization call copy constructor?

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.

What is default initialization in C++?

Default member initializer (C++11) [edit] This is the initialization performed when an object is constructed with no initializer.

What is initialization in constructor?

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.


1 Answers

This wording is added in the paper P0398R0, which is intented to describe the following case:

Z c = {};

for non-aggregate Z.

like image 179
xskxzr Avatar answered Oct 19 '22 11:10

xskxzr