rtti: missing sizeof() and alignof() in std::type_info

310 views
Skip to first unread message

Matthew Fioravante

unread,
Aug 6, 2015, 4:42:57 PM8/6/15
to ISO C++ Standard - Future Proposals
Is there any reason that sizeof(T) and alignof(T) are not available via the type_info struct from typeid()?

I want to use the address and size in particular to create a mapping of objects within other objects for debugging purposes. It doesn't actually appear to be possible in C++14 to get the size of the most dynamic type without rolling your own rtti solution.

What I would suggest is adding size() and align() methods to type_info to return sizeof(T) and alignof(T) respectively.

Nicol Bolas

unread,
Aug 6, 2015, 5:09:39 PM8/6/15
to ISO C++ Standard - Future Proposals
I wouldn't suggest calling it `size`, as that term is generally reserved for getting the number of elements in a container object. If we add a global `size` function that will call member `size` functions (assuming C++17 doesn't give us the ability to call member functions as if they were global), you wouldn't want `type_info` to respond to such a function call.

Perhaps `size_of` and `align_of`.

Andrey Semashev

unread,
Aug 6, 2015, 6:24:01 PM8/6/15
to std-pr...@isocpp.org
<bikeshed_mode>'object_size' and 'alignment'?</bikeshed_mode>

An interesting idea at the first glance, this would probably increase
type_info size overhead whether these functions are used or not, and I
expect them to be used rarely. Not sure this is worth it, provided
that an opt-in solution is possible.

Thiago Macieira

unread,
Aug 6, 2015, 7:43:36 PM8/6/15
to std-pr...@isocpp.org
Sounds like a reasonable proposal, but one tricky to implement.

In order to support polymorphic types linked before this feature, it needs to
support returning sizeOf() == alignOf() == 0 or another sentinel value that
indicates absence of information.

Also note that typeid(void) is valid, so what does typeid(void).sizeOf()
return?
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358

Reply all
Reply to author
Forward
0 new messages