I want to do runtime polymorphism and must know the actual type as well e.g. to count the instances of a certain derived class. Here is what I have so far:
header
struct Base {
virtual ~Base();
};
struct DerivedFoo : public Base {};
struct DerivedBar : public Base {};
source
Base::~Base() {}
int main() {
std::vector<std::unique_ptr<Base>> my_array{};
my_array.emplace_back(std::make_unique<DerivedFoo>());
my_array.emplace_back(std::make_unique<DerivedBar>());
for (const auto& elem : my_array) {
if (std::is_same<DerivedFoo, decltype(elem)>::value) {
// do something
}
// some testing
std::cout << typeid(decltype(elem)).name() << '\n';
std::cout << typeid(decltype(*elem)).name() << '\n';
std::cout << typeid(decltype(*elem.get())).name() << '\n';
}
return 0;
}
output
St10unique_ptrI4BaseSt14default_deleteIS0_EE
4Base
4Base
St10unique_ptrI4BaseSt14default_deleteIS0_EE
4Base
4Base
Problem is: I only manage to get the type of the base class. Not the derived one. So I never enter the if statement.
- I could add a virtual function "GetID()" or similar. But my feeling is that this is redundant. I should be able to do this with type traits?
- I looked into
std::unique_pointer<...>::element_type
andstd::pointer_traits
but could not get it running.
Any ideas?