I was trying to find some easy way to emplace elements in a std::vector<std::shared_ptr<int>>
but couldn't come with anything. std::shared_ptr
takes pointers as parameters, so I can still write this:
std::vector<std::shared_ptr<int>> vec;
vec.emplace_back(new int(10));
However, I don't want to use new
by hand if possible, and would rather like to use std::make_shared
if possible. The problem is that if I really want to use it, I have to use push_back
instead and lose the advantage of in-place construction:
std::vector<std::shared_ptr<int>> vec;
vec.push_back(std::make_shared<int>(10));
Is there any way to get the advantages of both emplace_back
and std::make_shared
? If not, is there a guideline I should follow in such a case?
EDIT: Actually, I asked this question, but had an unrelated problem. Andy's answer is the good one and there isn't any actual problem with using both emplace
functions and std::make_shared
at once.