C++ Logo


Advanced search

Re: [std-proposals] std::type_info::size()

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 1 Aug 2023 17:04:45 -0400
On Tue, Aug 1, 2023 at 4:52 PM Frederick Virchanza Gotham via Std-Proposals
<std-proposals_at_[hidden]> wrote:

> On Mon, Jul 31, 2023 at 4:48 PM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > What's more, changing this is a full-on language ABI break because it
> adds a
> > new field that isn't there in the type_info. That means the impact of
> *adding*
> > this now is extremely high, equal to replacing std:: with std2.
> Ok then here's a compromise to prevent ABI breakage: If you don't put
> it inside the 'type_info' object, then at least provide a way of
> retrieving it from any 'type_info' object, as follows:

In practice, type_info is not very useful for getting information about
types because there is an infinite universe of information you might want.
It's useful for comparing two types with each other, so that you can store
the information you actually want in a std::map or std::unordered_map and
then look it up using the type_info.

(Yes, you can get the mangled name from type_info::name, but there's
usually not much you can do with it other than eventually printing it out
for a human to read.)

For example, std::any needs to know not just the size of the object that it
stores, but also how to copy-construct and how to destroy that object. Will
you also propose adding methods `void copy_construct(void* dest, const
void* src)` and `void destroy(void* obj)` to the std::type_info class? And
if you propose adding those, then where do you draw the line? Will we end
up with a dozen compiler switches (which the implementors will have to
maintain) so that every user can get only the information they want in
their type_info objects?

And you can implement std::any without that kind of support from the
language. I'm guessing there's a way to do what you want to do as well,
without type_info needing to store the size.

> namespace std {
> size_t typeinfo_sizeof (type_info const &);
> size_t typeinfo_alignof(type_info const &);
> };
> int main(void)
> {
> std::type_info const &ti = typeid( SomeClass );
> cout << "Alignment of SomeClass: " << typeinfo_alignof(ti) << endl;
> }
> If you're worried about the overhead then compilers can have a switch
> "-fno-extended-typeinfo".
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

*Brian Bi*

Received on 2023-08-01 21:04:58