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: Sat, 13 Apr 2024 01:43:05 +0100
A day or two ago, I wrote:
>
> We may as well beef out std::type_info with the common
> subset of what's available on Microsoft and Itanium.


Back to the topic of extending std::type_info, here's what I'd like to
see in the future:
    If you look at the cppreference webpage for the <type_traits>
header, and discard all the deprecated one's, you're left with a list
of 64 booleans, such as is_arithmetic, is_polymorphic, and so on. Some
of these booleans are mutually exclusive, for example "is_compound ==
!is_fundamental", and so you can reduce that list further down below
64. The following can be removed:
    is_void - not needed because we can do "if ( ti == typeid(void) )"
    is_null_pointer - not needed because we can do "if ( ti ==
typeid(nullptr_t) )"
    is_compound - not needed because we can do !is_fundamental
    is_member_pointer - not needed because we can do
is_member_function_pointer || is_member_object_pointer

So we can fill a 64-Bit number, i.e an 'std::bitset<64>', with all
this information. Here's how the code would look:

#include <bitset> // bitset
#include <type_traits> // 60 booleans

class extended_type_info {
    template<typename T>
    friend extended_type_info typeidex(void) noexcept;
protected:
    std::bitset<60u> b;
public:
    bool is_compound() const noexcept { return !is_fundamental(); }
    bool is_member_pointer() const noexcept { return
is_member_function_pointer() || is_member_object_pointer(); }

    bool has_unique_object_representations() const noexcept { return b[0]; }
    bool has_virtual_destructor() const noexcept { return b[1]; }
    bool is_abstract() const noexcept { return b[2]; }
    bool is_aggregate() const noexcept { return b[3]; }
    bool is_arithmetic() const noexcept { return b[4]; }
    . . .
    . . .
    . . .
};

template<typename T>
extended_type_info typeidex(void) noexcept
{
    extended_type_info e;
    e.b[ 0] = std::has_unique_object_representations_v<T>;
    e.b[ 1] = std::has_virtual_destructor_v<T>;
    e.b[ 2] = std::is_abstract_v<T>;
    e.b[ 3] = std::is_aggregate_v<T>;
    e.b[ 4] = std::is_arithmetic_v<T>;
    . . .
    . . .
    . . .
    return e;
}

And here's the entire list of 60 + 2 traits written out exhaustively on GodBolt:

    https://godbolt.org/z/93Wo7qWPe

Received on 2024-04-13 00:43:19