Vector construction when it is wrapped in a generic pointer

So, I am working on a conversion from an OO language with garbage collection capabilities in C ++. To get started, I want to wrap all objects in shared pointers in order to solve the problem of clipping memory. Right now I'm trying to wrap a vector in a generic pointer and initialize the vector directly. See Question below. Why is this not working and, if possible, how can I make it work?

vector<int> vec({ 6, 4, 9 }); // Working

shared_ptr<vector<int>> vec = make_shared<vector<int>>({ 6, 4, 9 }); // Not working

Sorry to not include the error, the error I get is marked on (make_shared) and printed as:

no instance of function template "std::make_shared" matches the argument list
argument types are: ({...})

Thanks for any answers!

+4
source share
4 answers

.

, :

std::shared_ptr<std::vector<int>> vec = std::make_shared<std::vector<int>>(std::vector<int>{ 6, 4, 9 });
+5

make_shared. . : std:: make_shared std:: initializer_list

, , , - . , :

shared_ptr<vector<int>> vec(new vector<int>({ 6, 4, 9 }));
0
auto vec = make_shared<vector<int>>(std::initializer_list<int>{ 6, 4, 9 });
0

make_shared does not support implicit initializer lists. You can use the fact that it autocan list initializers:

auto init = { 6, 4, 9 };
auto vec = std::make_shared<std::vector<int>>(init); 

But, as pointed out by others, you need to think about whether you need a shared_ptrgeneral, vectormanages its own memory. shared_ptris not free.

0
source

All Articles