Fli*_*bed 8 c++ initialization vector shared-ptr
所以我正在进行从具有垃圾收集功能的OO语言到C++的转换.首先,我想将所有对象包装在共享指针中以解决内存解除分配问题.现在我试图将一个向量包装在共享指针中并直接初始化向量.请参阅下面的问题.为什么它不起作用,如果可能的话,我该如何使其工作?
vector<int> vec({ 6, 4, 9 }); // Working
shared_ptr<vector<int>> vec = make_shared<vector<int>>({ 6, 4, 9 }); // Not working
Run Code Online (Sandbox Code Playgroud)
很抱歉不包含错误,我得到的错误标记为(make_shared)并打印为:
no instance of function template "std::make_shared" matches the argument list
argument types are: ({...})
Run Code Online (Sandbox Code Playgroud)
谢谢你的回答!
Dra*_*rax 11
大括号初始化列表不能用于大多数类型推导上下文中.
如果您明确指定它的工作类型:
std::shared_ptr<std::vector<int>> vec = std::make_shared<std::vector<int>>(std::vector<int>{ 6, 4, 9 });
Run Code Online (Sandbox Code Playgroud)