dynamic_cast to derived type with unknown template argument

Viewed 1664

I would like to dynamic_cast to a templated derived type, with unknown template argument:

struct A {
    virtual void f() { };
};

template <std::size_t N>
struct B : public A { };

With the known template argument the cast can be performed as:

const A& base_ref = B<N>();
const B<N>& ref = dynamic_cast<const B<N>&>(base_ref);

My question is about the case where the value of the template argument (N) is not known.

I was wondering if there is a way to obtain the object type, in this case B<N> from the base class pointer/reference, such that the dynamic_cast template parameter can be inferred automatically?

P.S. In the considered case the template argument is an integer type, and is used as a template parameter for an std::array.

2 Answers
Related