我有下面的代碼,它不會編譯,它是星期五,我有點疲憊。C++ std :: unique_ptr存儲在std :: map中使用刪除函數生成
#include <string>
#include <memory>
#include <utility>
#include <map>
template< typename T, typename ...Args >
std::unique_ptr<T> make_unique(Args && ...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
struct A
{
};
std::map< std::string, std::unique_ptr<A> > _map = { { "A", make_unique<A>() } }; // <-- ERROR!!
以下編譯沒有問題
int main()
{
std::pair< std::string, std::unique_ptr<A> > p { "B", make_unique<A>() };
_map.insert(std::make_pair("C", make_unique<A>()));
}
我得到的錯誤是(粗略地除去克++絨毛)
use of deleted function 'constexpr std::pair<...>(const st::pair<...> &)
'constexp std::pair<...>::pair(const std::pair<...> &) is implicitly deleted because the default definition would be illegal.
Argghh !! 只需在C++ 11標準中閱讀。
當集合由初始化列表初始化,如在8.5.4指定 ,初始化器列表的元素被作爲 初始化爲聚集體的成員,在增加下標 或部件的順序。每個成員是副本初始化對應初始化子句
長號從 !!!
任何人都知道,如果這是簡單的不可能與初始化列表?
我真的需要這個來與初始化列表一起工作。編譯的代碼就是例子。 – ScaryAardvark
嗯。就是想。初始化列表是否要求它們的參數是可複製的?在這種情況下,錯誤是有道理的,因爲你不能複製一個unique_ptr – ScaryAardvark
[This](http://stackoverflow.com/questions/8193102/initializer-list-and-move-semantics)似乎相關.... –