I have a function which takes some vector of objects and filters it and needs to return the filtered vector of objects converted to a base class.
class Base {
// stuff
}
class Derived : public Base {
// more stuff
}
// elsewhere...
vector<reference_wrapper<const unique_ptr<Base>>> do_something(const vector<unique_ptr<Derived>> &things) {
vector<reference_wrapper<const unique_ptr<Base>>> a;
for (const unique_ptr<Derived> &derived : things) {
if (check(derived)) {
a.push_back(derived); // this obviously doens't work -
// what should I put here?!
// this does not work:
// a.push_back(std::ref(derived.get()))
// because derived.get() is an rvalue
}
}
return a;
}
The returned vector doesn't own any of the original objects - they should just be readers. The original objects will outlive the lifespan of the returned vector.