Does make_shared do a default initialization (zero-init) for each member variable

心不动则不痛 提交于 2019-12-05 01:06:51

If you see e.g. this std::make_shared reference you will see that

The object is constructed as if by the expression ::new (pv) T(std::forward<Args>(args)...), where pv is an internal void* pointer to storage suitable to hold an object of type T.

That means std::make_shared<Foo>() basically does new Foo(). That is, it value initializes the structure which leads to the zeroing of the non-class member variables.

To be more precise, std::make_shared employes value initialization syntax,

The object is constructed as if by the expression ::new (pv) T(std::forward<Args>(args)...)

For Foo with an empty initializer list, that means all its members with built-in type will be zero-initialized, and std::string will be default-initialized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!