Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does `std::make_shared<POD>()` value initialize my POD?

Does std::make_shared<POD>() value initialize my POD?

If yes, is this a guaranteed by the standard?

If no (as I suspect), is there a way to do this? I guess std::make_shared<POD>(POD()) would do but is that what I should be doing?

like image 521
Gurg Hackpof Avatar asked Jun 14 '13 08:06

Gurg Hackpof


1 Answers

Yes, it's value intialized, and this is guaranteed by the standard:

§20.7.2.2.6,2: (about make_shared)

Effects: Allocates memory suitable for an object of type T and constructs an object in that memory via the placement new expression ::new (pv) T(std::forward<Args>(args)...).

And §5.3.4,15:

A new-expression that creates an object of type T initializes that object as follows:
— If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.
Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for directinitialization.

So it's direct-initialized as in new POD().

§8.5,16:

The semantics of initializers are as follows. [...]
— If the initializer is (), the object is value-initialized.

like image 159
Arne Mertz Avatar answered Oct 10 '22 23:10

Arne Mertz