How to declare a vector of unique_ptr's as class data member?

后端 未结 5 1322
借酒劲吻你
借酒劲吻你 2021-02-20 11:26

I\'d like to have a vector of unique_ptr\'s as a member of a class I\'m making.

class Foo {
    [...]

private:
    vector> barList;
         


        
5条回答
  •  囚心锁ツ
    2021-02-20 11:59

    An excerpts from www.cplusplus.com

    std::unique_ptr::operator=
    

    unique_ptr assignment The object acquires the ownership of x's content, including both the stored pointer and the stored deleter (along with the responsibility of deleting the object at some point). Any object owned by the unique_ptr object before the call is deleted (as if unique_ptr's destructor was called).

    But there is a warning too:

    This page describes a feature introduced by the latest revision of the C++ standard (2011). Older compilers may not support it.

    MSVC 2010 defines operator= as private (non-copyable) but supports swap method.

提交回复
热议问题