There is is_base_of to check if a class A is a base class of B. But what if I don't have the type of a variable?
Let's say I have five classes A, B, C, D and E, and D and E are derived from both A and B respectively. C is only derived from A. Now I have a pointer to A and I want to know if I can cast the pointer to B. In this case I want to know if my A pointer points to a D or E object, so that I can cast the pointer to a B object.
I tried the following which did not work.
void foo(const std::shared_ptr<A> & ptr) {
if (std::is_base_of<B, decltype(*ptr)>::value) {
doSomething(std::static_pointer_cast<B>(ptr));
}
}
Edit: foo is not a template function, A and B are just placeholders for my existing classes.