Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error with vector of deque of unique_ptr

Tags:

c++

c++11

The following code fails to compile on gcc 5.3 with compiler error complaining that the copy constructor of the unique_ptr is somehow invoked. Can someone explain why this happens?

#include <iostream>
#include <memory>
#include <deque>

using Foo = std::deque<std::unique_ptr<int>>;                                   


void foo() {                                                                    
  std::vector<Foo> a;                                                           
  a.emplace_back();   // this fails to compile                                                             
}

The key line in the compiler error is:

gcc-4.9.2/include/c++/4.9.2/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 = int; _Dp = std::default_delete<int>]’ { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }

like image 995
user2736667 Avatar asked Apr 02 '16 00:04

user2736667


People also ask

Can unique_ ptr be copied?

A unique_ptr does not share its pointer. It cannot be copied to another unique_ptr , passed by value to a function, or used in any C++ Standard Library algorithm that requires copies to be made. A unique_ptr can only be moved.

What is a unique_ ptr in c++?

(since C++11) std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

How does unique_ ptr work?

A unique_ptr object wraps around a raw pointer and its responsible for its lifetime. When this object is destructed then in its destructor it deletes the associated raw pointer. unique_ptr has its -> and * operator overloaded, so it can be used similar to normal pointer.


1 Answers

  • vector::emplace_back needs to handle reallocation.
  • on vector reallocation, the existing elements are moved if either

    • they cannot be copied (as determined by std::is_copy_constructible); or
    • their move constructor is noexcept.

    otherwise they are copied. This is to maintain the strong exception safety guarantee.

  • std::deque<std::unique_ptr<int>>'s move constructor is not noexcept (depending on the implementation, it may need to allocate memory).
  • std::deque<std::unique_ptr<int>> can be copied according to std::is_copy_constructible, because that only checks if there is a constructor with the right signature, not whether the body of said constructor will actually compile.
  • vector::emplace_back therefore attempts to copy it.
  • the compiler blows up.
like image 127
T.C. Avatar answered Sep 22 '22 18:09

T.C.