#Order of object creation

21 messages · Page 1 of 1 (latest)

grizzled spruce
#

If I have something like:

String text = String(“hello”);

What goes on? Is text default constructed then String hello creates a temp object and then it gets copied into text?

tacit cedarBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cunning palm
#

I'm 99% sure it will just instantiate the object a single time.

Either that or the String("hello") will create a string, then text will be move constructed using that temporary.

I am almost certain. This would be a case of return value optimization, so the String("hello") will construct directly to text

#

Key point is that there is NO default construction happening.

type name = value;

is fundamentally different from

type name;
name = value;

At least for class types anyway

fresh hawk
cunning palm
#

Assume it isn't optimized, then yes

#

But 99% sure it is optimized away in this case

real sandal
#

it's guaranteed by the standard

fresh hawk
#

Yea, true, I know about that elision thing

fresh hawk
#

Wait, I think i misspoke, shouldn't it always be calling that constructor?

real sandal
#

it won't call the move constructor in your original example

cunning palm
#

Basically the construction of would have been the temporary is done to text directly

fresh hawk
#

I mean, not for the String("hello") bit, but it would've been called for text tho right?

fresh hawk
#

My understanding of that bit of code its equivalent to

String text("hello");
cunning palm
fresh hawk
cunning palm
fresh hawk
#

oh wait true