c++ - 为什么不能使用 initializer_list 来初始化 unique_ptr 的 vector ?

标签 c++ c++11 unique-ptr initializer-list

<分区>

我想知道为什么 initializer_list 不能与 unique_ptr 一起使用:

std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};

不编译。

但是:

std::vector<std::unique_ptr<int>> vptr(2);
vptr[0] =std::make_unique<int>(1);
vptr[1] =std::make_unique<int>(2);

编译。

std::vector<int*>vint={new int{1},new int{2}};

 std::vector<std::shared_ptr<int>> vptr= {std::make_shared<int>(1), std::make_shared<int>(2)};

编译。

unique_ptr 变体在 clang 中给出此错误消息:

In file included from main.cpp:2:
In file included from /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/vector:62:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_construct.h:75:38: error: call to deleted constructor of 'std::unique_ptr<int, std::default_delete<int> >'
    { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
                                     ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:75:8: note: in instantiation of function template specialization 'std::_Construct<std::unique_ptr<int, std::default_delete<int> >, const std::unique_ptr<int, std::default_delete<int> > &>' requested here
                std::_Construct(std::__addressof(*__cur), *__first);
                     ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:125:2: note: in instantiation of function template specialization 'std::__uninitialized_copy<false>::__uninit_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
        __uninit_copy(__first, __last, __result);
        ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_uninitialized.h:278:19: note: in instantiation of function template specialization 'std::uninitialized_copy<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *>' requested here
    { return std::uninitialized_copy(__first, __last, __result); }
                  ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:1284:11: note: in instantiation of function template specialization 'std::__uninitialized_copy_a<const std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > *, std::unique_ptr<int, std::default_delete<int> > >' requested here
            std::__uninitialized_copy_a(__first, __last,
                 ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/stl_vector.h:377:2: note: in instantiation of function template specialization 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::_M_range_initialize<const std::unique_ptr<int, std::default_delete<int> > *>' requested here
        _M_range_initialize(__l.begin(), __l.end(),
        ^
main.cpp:10:42: note: in instantiation of member function 'std::vector<std::unique_ptr<int, std::default_delete<int> >, std::allocator<std::unique_ptr<int, std::default_delete<int> > > >::vector' requested here
std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)};
                                         ^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
      unique_ptr(const unique_ptr&) = delete;
      ^
1 error generated.

最佳答案

vector<T> 的构造函数服用initializer_list<T>将初始化列表中的元素复制到 vector 中。当您处理不可复制的类型时,这不起作用:unique_ptr<T> 的设计防止复制。它确实允许移动,这就是分配有效的原因:make_unique返回一个右值,编译器知道它可以安全移动。

你需要某种构造函数来接受 initializer_list<T &&> , 但 C++ 没有这样的构造函数(实际上,正如 dyp 指出的那样,类型 initializer_list<T &&> 甚至没有函数)。

关于c++ - 为什么不能使用 initializer_list 来初始化 unique_ptr 的 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26389595/

相关文章:

c++ - 为什么 unique_ptr 不能推断删除器的类型?

c++ - 当我们使用 mpi_send/receive 函数时到底发生了什么?

c++ - 为什么 nvcc 对仿函数不满意

qt - 接口(interface)类纯虚信号的连接

c++ - std::remove_if 是否保证按顺序调用谓词?

c++ - 为什么 unique_ptr::release 没有用 [[nodiscard]] 定义?

c++ - OpenGL 抗锯齿不起作用

c++ - 将代码从 Windows 移植到 Linux 时删除数组时出错

c++ - std::function 参数列表和 typedef

c++ - 将 SDL_Cursor 与 unique_ptr : error incomplete type is not allowed 一起使用