Consider the code snippet below:
#include <vector>
class Base {};
class Derived : public Base {};
std:vector<std:unique_ptr<Base>> baseVector;
I want to down-cast the vector
to vector<unique_ptr<Derived>>
The naive approach would be:
- iterating over the
baseVector
- for each element of type
Base
,dynamic_cast
it toDerived
class - add a casted element to the new vector of type
vector<unique_ptr<Derived>>
Is there a simple way to down-cast the baseVector
to vector<unique_ptr<Derived>>
all together (one line)?