C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Base class reflection

From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Date: Mon, 30 Jan 2023 14:12:28 +1100
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2965.html

On Mon, Jan 30, 2023 at 8:20 AM Billy Martin via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello, I would like to propose an addition to the standard library, that
> will allow one to get the immediate base classes of a type, e.g.:
>
> using std::base_class_tuple_t<T> = std::tuple<Bases&...>;
>
> Requires: T is a complete class type.
> Result: an std::tuple type, where the tuple element types are the
> reference types to the immediate base classes of T, if T is a class type
> with base classes, or the empty tuple type otherwise. The references would
> have the same cv-qualification as T.
>
> Why: There is currently no way to do this (that I'm aware of) in C++. It
> would require some compiler magic to implement this type. (note: I don't
> know if it's customary or not to have compiler magic in the standard
> library, maybe this could be a keyword instead, similar to typeid)
>
> This is necessary to solve the problem of mixing polymorphism and type
> erasure. Suppose I have a type erased pointer to some polymorphic object of
> type D. I don't know about the type D. But I know the type B, which is a
> base class of D. I would like to get a B* pointer to D. In general, this
> can't be done as far as I know. (Usually with type erasure you need to know
> the *exact* original type of the type erased thing)
>
> However, if std::base_class_tuple_t exists, then the type erasure system
> could be set up so that you could request a type erased pointer to B, and
> it could search the base classes of D for the type B, and if it finds it
> then perform the pointer conversion and return a type erased pointer to B,
> which would could then extract B* from.
>
> It seems like this wouldn't be too hard to implement since the compiler
> ought to know the immediate base classes of a complete type.
>
> Why reference types? Some of the bases might be pure virtual, and I don't
> yet know how this might fit into a greater reflection library. It should be
> possible, at least in principle, to create an object of type
> std::base_class_tuple_t. Maybe we could also someday add other tuple types
> to ask other questions about what the compiler knows about a type? Maybe a
> non-static member tuple?
>
> Billy
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-01-30 03:12:45