1

When I declare a vector of unique_ptr's, I get this kind of error:

d:\qt\mingw64\include\c++\4.8.0\bits\stl_construct.h:75: 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>]'

Which looks like the classical error of creating a containers of objects which have no copy constructor.

However, it is documented in everything I could find that a standard container of unique_ptrs works thanks to the c++11 move semantics.

I am compiling with MinGW-gcc 64-bit, using -std=gnu++11.

Is it supported only in c++11 and not in gnu++11?

Thanks

9
  • 1
    Why don't you try with c++11? Commented Apr 1, 2014 at 14:04
  • 4.8.0 it's not C+11-compliant, any release starting from the 4.8.1 is Commented Apr 1, 2014 at 14:05
  • 1
    Look the following link: stackoverflow.com/q/10613126/2724703 Commented Apr 1, 2014 at 14:06
  • 1
    Show us a minimal example program that demonstrates your exact error so we can try it. Commented Apr 1, 2014 at 14:08
  • 1
    I just figured that the std::vector<std::unique_ptr<int> > is declared as a member variable in a class. Since the class has a copy constructor, and if I understand right, this is the issue. Commented Apr 1, 2014 at 14:11

2 Answers 2

2

The following will compile with C++11.

#include <iostream>
#include <vector>
#include <memory>
using namespace std;

int main()
{
    std::vector<std::unique_ptr<int> > asdf;
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem was not std::vector<std::unique_ptr<int> > itself, but the member variable of this type declared in a copiable class. Since the default copy constructor of the class calls the copy constructor of std::vector, which in turns calls the default constructor of std::unique_ptr, the later being deleted, compilation fails.

std::vector<std::unique_ptr<int> > compiles fine as a local variable in a function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.