c++11 - Why does std::make_shared<class T, class...Args>(Args&&... args) require T to have copy constructor? -
i don't understand why class has deleted copy constructor (or contains member, ifstream, has deleted copy constructor , there it, too, has deleted copy constructor) can't used make_shared()? code shows i'm talking
class x { private: x(const x&) = delete; int x; public: x(int an_int) : x{an_int} {}; x() : x{10} {} }; int main(int argc, char** argv) { // fails because x has no copy constructor shared_ptr<x> ptr { make_shared<x>( x{8} ) }; shared_ptr<x> ptr2 { new x{10} };// works fine return 0; }
you may missing fact make_shared
forward arguments constructor of x. in case passing x{8}
constructor argument, make_shared
forced attempt copy or move construction.
in particular example, deleting copy constructor has implicitly deleted move constructor, preventing construction temporary x{8}
.
what want write this:
shared_ptr<x> ptr { make_shared<x>(8) };
Comments
Post a Comment