copy-initialization

When should you use direct initialization and when copy initialization?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 06:01:31
Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization The actual names of the things you describe is not implicit and explicit assignment but : Copy-initialization : T x = a; Direct-initialization : T x(a); They are not equivalent, most notably in contexts where a conversion is required, for example when T is of class type and a is of a different type (see Alf comment for examples of contexts which don't even involve conversion).

Does copy list initialization invoke copy ctor conceptually?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 18:09:08
问题 Before C++11, we can do copy initialization by writing something like A a = 1; which is more or less equivalent to A a = A(1); . That is, a temporary is first created and then a copy ctor is invoked. Regardless of copy elision, this must be so conceptually and the copy ctor must be accessible. With list initialization in C++11, we can do a copy list initialization by writing A a = {1, 2}; . In my opinion, this should be more or less equivalent to A a = A(1, 2); . However, on GCC and clang, A

When should you use direct initialization and when copy initialization?

柔情痞子 提交于 2019-11-27 01:09:41
问题 Is it simply preference or are there specific instances where one is necessary over another? I'm refering to the following variants for initialization T t(e); // direct initialization T t = e; // copy initialization 回答1: The actual names of the things you describe is not implicit and explicit assignment but : Copy-initialization : T x = a; Direct-initialization : T x(a); They are not equivalent, most notably in contexts where a conversion is required, for example when T is of class type and a