C++ Logo


Advanced search

Re: [std-proposals] Base class reflection

From: Billy Martin <bmartin_at_[hidden]>
Date: Sun, 29 Jan 2023 17:46:03 -0800
You can't do this because there is no code that knows about both the type D
and the type B at compile time.

However! If the feature I am requesting exists, then you could pass a
type-erased pointer to B to the code that knows about D and say, "set this
pointer to point to the object of interest, please".
Then, that class, that knows the type D, can say, "oh? what's this? A
type-erased pointer to one of D's base classes? No problem! let me just
figure out which base class it is and I'll go ahead and static_cast it to
that type for you."
It would then go through the list of base classes using
std::base_class_tuple_t (recursively) until it finds the right one to undo
the type erasure, and then it will have both the type B and D and can do
the cast.

The reason I want to do this is because I have a smart pointer
implementation, similar to std::shared_ptr, and I'm trying to implement
basically std::shared_ptr::clone() for all copyable types. Type erased
things generally know how to copy themselves just fine, but converting back
to the original pointer type is problematic.


On Sun, Jan 29, 2023 at 4:49 PM Jonathan Tanner <
jonathan.tanner_at_[hidden]> wrote:

> What is the difference between a type erased pointer to B and B*, why
> can’t you just obtain the B* when you know the concrete type D with a
> static_cast and just store that?

Received on 2023-01-30 01:46:15