In this question it was suggested to use std::shared_ptr
instead of std::unique_ptr
. However I am not able to make this work when feeding this shared pointer to my function:
std::vector<std::unique_ptr<segment>> v3;
//VERSION 5:
auto myLine2 = std::make_shared<Line>(start, end);
add_segment(v3, myLine2);
Here's the new implementation of the function:
template<typename T1, typename T2>
inline void add_segment(T1 & v, T2& line_or_circle)
{
v.emplace_back(std::move(line_or_circle));
}
What am I doing wrong here?
You can't "move" the thing inside a shared_ptr
into an instance of a unique_ptr
. If you could, all kinds of problems would occur as a result of two different smart pointers trying to delete the thing inside it when they get released.
Instead, just delcare your vector as :
std::vector<std::shared_ptr<segment>> v3;
And then, you really don't need move semantics after that:
template<typename T1, typename T2>
inline void add_segment(T1 & v, T2& line_or_circle)
{
v.emplace_back(line_or_circle);
}