Coffeehouse Post

Single Post Permalink

View Thread: MS working on a same compiler for C++ AND C# ! Not in 'incubation' but for production !
  • User profile image
    banjomatic

    @SteveRichter: unique_ptr is non-copyable by design as the unique_ptr owns the memory. If you want to transfer ownership, you need to std::move otherwise you'd have two unique_ptr's owning the same data (violating the uniqueness).

    If you want to share ownership of memory between multiple smart pointers, you would use shared_ptr instead as its reference count is adjusted on copy/assignment, etc.

    As for the std::vector of std::unique_ptr, this can be done with emplacement, this will construct the unique_ptr inside the vector itself with the raw pointer passed in. But i think with move semantics, std::vector<std::wstring> might be more appropriate.

     

    I'm still figuring out all of the c++11 guidance myself, but maybe something like this:

    std::wstring msg(L"The unique_ptr class supersedes auto_ptr");
    ::OutputDebugString(msg.c_str( ));
    msgList.push_back(std::move(msg));