If I have a struct and I initialize it like so:
#include <memory>
struct MyHandle
{
std::shared_ptr<int> handle_;
};
int main()
{
MyHandle m{std::make_shared<int>(42)};
}
Is the fact that aggregate initialization of MyHandle occurs so no constructor is used to initialize object of type MyHandle?
MyHandle is not a POD, because a POD can't contain non-POD members (and shared_ptr is not a POD). shared_ptr's constructor is definitely called when constructing a MyHandle object.
That's correct. Aggregate initialisation is only allowed for classes with no user-provided constructors, and (in the words of the standard, C++11 8.5.1/2), "each member is copy-initialised from the corresponding initialiser-clause". So no constructor for MyHandle
is used, only a copy, move or conversion constructor for each member of class type.
The implicit default constructor, which default-initialises each member, is used for default and value initialisation; but it can't be used for aggregate initialisation since each member can only be initialised once.
Apparently std::shared_ptr is not POD, you could use std::is_pod to check POD type:
std::is_pod<std::shared_ptr<int>>::value
should return 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.