There are many things wrong here:
return (std::unique_ptr<IFC>(make_unique<IFC>(m_shpVectorIFC.pop_back())));
Let us break it down into multiple lines, C++11 style. This isn't quite equivalent, but close:
auto&& a = m_shpVectorIFC.pop_back();
auto&& b = make_unique<IFC>(std::move(a));
auto&& c = std::unique_ptr<IFC>(std::move(b));
return std::move(c);
which isn't a bad technique to start with.
Now you'll get an error that will be more informative.
The first problem is that pop_back()
returns void
. Fix that with:
auto a = std::move(m_shpVectorIFC.back()); // note a value, as we are about to mutate the vector
m_shpVectorIFC.pop_back();
auto&& b = make_unique<IFC>(std::move(a));
auto&& c = std::unique_ptr<IFC>(std::move(b));
return std::move(c);
we still run into problems. IFC
cannot be constructed from a std::shared_ptr<IFC>&&
. It cannot. We can make a copy:
auto a = std::move(m_shpVectorIFC.back());
m_shpVectorIFC.pop_back();
auto&& b = make_unique<IFC>(*a);
auto&& c = std::unique_ptr<IFC>(std::move(b));
return std::move(c);
Next, auto&&c
is pointless.
auto a = std::move(m_shpVectorIFC.back());
m_shpVectorIFC.pop_back();
auto&& b = make_unique<IFC>(*a);
return std::move(b);
and we might as well store b
as a value:
auto a = std::move(m_shpVectorIFC.back());
m_shpVectorIFC.pop_back();
auto b = make_unique<IFC>(*a);
return b;
then some error detecting:
auto a = std::move(m_shpVectorIFC.back());
m_shpVectorIFC.pop_back();
if (!a)
return {};
auto b = make_unique<IFC>(*a);
return b;
and micro-optimize:
auto a = std::move(m_shpVectorIFC.back());
m_shpVectorIFC.pop_back();
if (!a)
return {};
auto b = a.unique()?make_unique<IFC>(std::move(*a)):make_unique<IFC>(*a);
return b;
Now, this doesn't return the shared_ptr
data, but rather a copy of it. You cannot move the guts of a C++11 shared_ptr
into a unique_ptr
. You can do it the other way.
Odds are you should be storing a unique_ptr
in your vector anyhow.