C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extend std::type_info with more information

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 15 Apr 2024 07:37:40 +0100
On Sun, Apr 14, 2024 at 3:14 PM Sebastian Wittmeier wrote:
>
> So which use would it be to get more type traits?
> We cannot construct or copy or call member functions or access member variables.


If the "extended_type_info" looked like as follows:

        class std::extended_type_info {
            uint_least64_t traits;
            size_t size_of, align_of;
        };

Then, if the 64-Bit integer has the bit set for
"is_trivially_copyable", then we can just memcpy:

    void CopyConstruct( void *const dst, void const *const src,
extended_type_info const &eti )
    {
        assert( eti.traits & 0x2000000000000 ); // make sure bit is
set for is_trivially_copyable
        assert( 0u == ((uintptr_t)dst % eti.align_of) ); // make
sure destination buffer is aligned
        memcpy( dst, src, eti.size_of );
    }

But even for types that aren't trivially-copyable, we could store the
address of the copy-constructor inside the extended_type_info (which
is actually what std::any does in order to make it copyable):

        class std::extended_type_info {
            void (*address_of_copy_constructor)(void*,void const*);
            uint64_t traits;
            size_t size_of, align_of;
        };

      void CopyConstruct( void *const dst, void const *const src,
extended_type_info const &eti )
      {
          eti.address_of_copy_constructor(dst,src);
      }

Of course while we're at it, we could store the address of the
destructor in there as well. We could probably make the
extended_type_info a hundred bytes long . . . maybe even a kilobyte.
I'm just floating this idea here to see what people are interested in
having inside an 'extended_type_info', because at the moment
'type_info' is very sparse.

I think a good place to start would be to have a 64-Bit integer with
all the boolean type_traits.


> Extending runtime type information is an ABI break and potentially
> wasteful for programs with lots of generated types.


Two ways to prevent ABI breaking when extending type_info:

    (1) Have a totally separate type and a totally separate operator

        class std::extended_type_info { uint64_t traits; size_t
size_of, align_of; };
        int main(void)
        {
            std::extended_type_info const &eti = typeidex(int);
        }

    (2) Derive a type from std::type_info, and use dynamic_cast to find it

        class std::extended_type_info : public std::type_info {
uint64_t traits; size_t size_of, align_of; };
        int main(void)
        {
            std::extended_type_info *eti =
dynamic_cast<extended_type_info const*>( &typeid(int) );
            if ( nullptr != eti ) DoSomething( *eti );
        }

Received on 2024-04-15 06:37:54