std::meta::type_order - cppreference.com
Namespaces
Variants

std::meta::type_order

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Type properties
Type property queries
 
Defined in header <meta>
consteval std::strong_ordering type_order( std::meta::info r1, std::meta::info r2 );
(since C++26)

Performs a three-way comparison on reflected types. The total ordering of all types is implementation-defined.

Returns std::type_order<T1, T2>::value, where T1 and T2 are the types represented by std::meta::dealias(r1) and std::meta::dealias(r2), respectively.

This ordering need not be consistent with the one induced by std::type_info::before.

The template arguments T1 and T2 can be incomplete types.

Parameters

r1, r2 - reflection values

Return value

The std::strong_ordering value equal to

  • std::strong_ordering::less if T1 precedes T2 in the implementation-defined total order,
  • std::strong_ordering::greater if T2 precedes T1, and
  • std::strong_ordering::equal if they are the same type.

Exceptions

Throws std::meta::exception if r1 or r2 does not represent a type or type alias.

Example

#include <algorithm>
#include <compare>
#include <meta>
#include <print>
#include <vector>

consteval auto sort_types(std::vector<std::meta::info>&& types)
{
    std::ranges::sort(types, [](std::meta::info a, std::meta::info b)
    {
        return std::meta::type_order(a, b) == std::strong_ordering::less;
    });
    return std::define_static_array(types);
}

int main()
{
    static constexpr auto types{ sort_types({^^double, ^^int, ^^char, ^^int}) };
    template for (constexpr auto type : types)
        std::print("{} ", std::meta::display_string_of(type));
    std::println();
}

Possible output:

char double int int

See also