Regarding the advice to avoid dynamic_cast, message definitely received, but the main challenge is that some_method actually has a return type of DerivedA for its implementation in DerivedA, and DerivedB for its implementation in DerivedB. And this return type is precisely what I am trying to cast to.
Perhaps I skipped important contextual info in my effort to simplify my example. To be specific,
Base is a base class that wraps a matrix data structure, for example, from an external linear algebra library.
DerivedA is an implementation to wrap dense matrices from a particular linear algebra library.
DerivedB is an implementation for sparse matrices from a particular linear algebra library.
Users should be able to extend this and create their own wrappers for their favorite linear algebra library, e.g., DerivedC: public Base, and then they can use DerivedC everywhere else in my library, thanks to runtime polymorphism.
operation might be matrix multiplication, for example, but I want to allow for mixed sparse-dense multiplication.
some_method returns a reference to the underlying matrix data, with the specific type DerivedA or DerivedB. Base doesn't know this, so I cannot define a virtual some_method in Base.
As a less terrible compromise, I've made an intermediate DerivedX: public Base which is a template that takes the type of the underlying matrix wrapped by DerivedA and DerivedB, implements the dynamic cast, but also replicates each operation with a templated version of the operation, to allow mixed operations.