Thread: Vector of `std::unique_ptr' initialization

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    66

    Vector of `std::unique_ptr' initialization

    Hi,

    Code:
    struct base {
        virtual ~base()=default;
    };
    
    
    template<typename... Types>
    class collection {
        std::vector<std::unique_ptr<base>> m_values{std::unique_ptr<base>{new Types}...};
    };
    
    
    struct test: base {
    };
    
    
    int main()
    {
        collection<test> c;
    }
    GCC gives me
    Code:
    /usr/include/c++/5.1.0/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = base; _Dp = std::default_delete<base>]’
         { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
    I know that `std::unique_ptr' is movable only, but I can't figure out why this program is trying to copy construct one?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I know that `std::unique_ptr' is movable only, but I can't figure out why this program is trying to copy construct one?
    O_o

    You should do what I told you last time.

    You should remove the extraneous wrapping which is hiding the issue.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Ok, I could also have used the code below but changing the context doesn't change the problem (I guess). The program still tries to call the deleted copy constructor:

    Code:
    struct base {
        virtual ~base()=default;
    };
    
    
    struct test: base {
    };
    
    
    int main()
    {
        std::vector<std::unique_ptr<base>> values{std::unique_ptr<base>{new test}};
    }
    As far as I know,
    Code:
    std::unique_ptr<base>{new test}
    is a rvalue and should therefore be moved into the vector instead of being copied. Obviously I'm using the unique pointer the wrong way, but i still don't understand why... :/

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Ok I think I figured it out. The problem is the initializer list of the vector, which can't move objects but only copy them. Therefore using the initializer list won't work for objects of type `std::unique_ptr'.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Ok I think I figured it out. The problem is the initializer list of the vector, which can't move objects but only copy them. Therefore using the initializer list won't work for objects of type `std::unique_ptr'.
    O_o

    Good on you...

    `std::unique_ptr'
    also you may have had your limit of exposure to me.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  2. Replies: 8
    Last Post: 01-27-2013, 06:38 AM
  3. C++11 Vector class initialization
    By EVOEx in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2012, 09:51 AM
  4. std::unique_ptr/shared_ptr assignment semantics
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2012, 11:25 AM
  5. std::unique_ptr
    By KIBO in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2010, 07:48 AM