Namespaces
Variants

std::type_order

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
Defined in header <compare>
template< class T, class U >
struct type_order;
(since C++26)

Implements an implementation-defined total ordering of all types.

Provides the member constant value equal to std::strong_ordering::less if T precedes U in this implementation-defined total order, std::strong_ordering::greater if U precedes T, and std::strong_ordering::equal if they are the same type.

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

The template arguments T and U can be incomplete types.

If the program adds specializations for std::type_order or std::type_order_v, the program is ill-formed.

Helper variable template

template< class T, class U >
constexpr std::strong_ordering type_order_v = type_order<T, U>::value;
(since C++26)

Member constants

value
[static]
a value of type std::strong_ordering, as described above
(public static member constant)

Member functions

operator std::strong_ordering
converts the object to std::strong_ordering, returns value
(public member function)
operator()
returns value
(public member function)

Member types

Type Definition
value_type std::strong_ordering

Notes

Feature-test macro Value Std Feature
__cpp_lib_type_order 202506L (C++26) std::type_order, std::type_order_v

Example

#include <compare>
#include <meta>
#include <print>
#include <type_traits>

template <typename T, typename U>
void print_type_order()
{
    auto t{std::meta::display_string_of(^^T)};
    auto u{std::meta::display_string_of(^^U)};
    if constexpr (constexpr auto order{ std::type_order_v<T, U> }; order == 0)
        std::println("“{}” and “{}” have the same order", t, u);
    else
        std::println("“{}” goes before “{}”", order < 0 ? t : u, order < 0 ? u : t);
}

int main()
{
    template for (constexpr auto t : {^^int, ^^char, ^^int&, ^^const int, ^^long})
        print_type_order<int, typename[:t:]>();
}

Possible output:

“int” and “int” have the same order
“char” goes before “int”
“int&” goes before “int”
“const int” goes before “int”
“int” goes before “long int”

See also

the result type of 3-way comparison that supports all 6 operators and is substitutable
(class) [edit]
performs a three-way comparison on reflected types
(function) [edit]