Tôi có mã này để khởi tạo bản đồ từ thành unique_ptr.Làm thế nào để đồng bộ khởi tạo bản đồ của unique_ptr?
auto a = unique_ptr<A>(new A());
map<int, unique_ptr<A>> m;
m[1] = move(a);
Tôi có thể sử dụng đồng bộ khởi tạo điều này không? Tôi đã thử
map<int, unique_ptr<A>> m {{1, unique_ptr<A>(new A())}};
Nhưng tôi gặp lỗi.
Một số phần của thông báo lỗi là
In instantiation of 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<A, std::default_delete<A> > >&}; _Val = std::pair<const int, std::unique_ptr<A> >]': ... In file included from /opt/local/include/gcc48/c++/memory:81:0,
from smart_pointer_map.cpp:3: /opt/local/include/gcc48/c++/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
Lỗi là gì? Chương trình có gặp sự cố khi bạn chạy chương trình không? –
có thể trùng lặp của [Tại sao tôi không thể đẩy \ _back duy nhất \ _ptr vào một vectơ?] (Http://stackoverflow.com/questions/3283778/why-can-i-not-push-back-a-unique- ptr-in-a-vector) –