'C++: Copy constructor call using curly braces
Suppose I have the following piece of code where the only things about Type I know are:
Typedoes not implement constructor with signatureType(std::initializer_list<Type>)i.e. constructor taking an initalizer list consisting ofTypeelements as its argumentTypedoes implement default and copy constructors
/*
all necessary includes
*/
int main()
{
Type t{}; //calls default c-tor
Type t_ref(t); //Reference case. calls copy c-tor
Type t1{t}; //Case 1. is there any difference from Reference case?
Type t2 = Type{t}; //Case 2. is there any difference from Reference case?
Type t3{Type{t}}; //Case 3. is there any difference from Reference case?
Type t4 = {t}; //Case 4. is there any difference from Reference case?
}
I know curly braces are used for initializer lists, but I have not found how they behave by the standard in cases shown in the code above.
Can I be 100% sure all four cases behave exactly the same in all possible cases as the Reference case? If not, when and what is the difference?
Are there any drawbacks of using curly brackets over simple brackets when copy constructing an object in C++?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
