C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 01 Aug 2023 14:56:48 -0700
On Tuesday, 1 August 2023 13:52:03 PDT Frederick Virchanza Gotham via Std-
Proposals wrote:
> 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:
>
> namespace std {
> size_t typeinfo_sizeof (type_info const &);
> size_t typeinfo_alignof(type_info const &);
> };

That's still a binary-incompatible change because it has to store the value
*somewhere* and the full size of the type_info object is already taken. You
can't add to the end because classes derived from std::type_info like
abi::__class_type_info have placed additional members at those offsets.

The implementation could prepend those members, but without having a bit
somewhere indicating that it's an extended type, the two functions that you're
proposing above would access garbage data on any type_info that hasn't been
compiled with the new compiler option. So this would be a feature unsafe to
use.

> If you're worried about the overhead then compilers can have a switch
> "-fno-extended-typeinfo".

Switch it around and make it -fenable-extended-typeinfo. That is, make it opt-
in. Most of us don't need it and therefore don't want to pay the overhead
cost. Then, whoever wants this needs to ensure that they use it only on types
that have been compiled with such a flag.

If you want to argue that the default should be opt-out, then go ahead and
make your argument. It should include a comparison to a library-only solution,
without compiler intervention. After all, what you're requesting is already
possible with QMetaType:

$ cat /tmp/testmeta.cpp
#include <QtCore/QMetaType>
#include <QtCore/QString>
#include <iostream>
struct alignas(32) S { int i[3]; };
void printType(QMetaType t)
{
    std::cout << t.name() << ": size=" << t.sizeOf() << "; align=" <<
t.alignOf() << '\n';
}
int main(int argc, char *argv[])
{
    printType(QMetaType::fromType<QString>());
    printType(QMetaType::fromType<S>());
}
$ /tmp/testmeta
QString: size=24; align=8
S: size=32; align=32

Note how it also carries a "pretty" name for the type, unlike std::type_info,
which will give the mangled name "1S" and "7QString".
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-08-01 21:56:50