Вопрос

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

Это было полезно?

Решение 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.

Другие советы

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top