std::common...son_category(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay so bestie, welcome to the std::common...son_category(3) page from the C++ Standard Libary β€” yes, the header literally spells it C++ Standard Libary, and the title truncates to std::common...son_category(3) like it ran out of breath mid-sentence πŸ’€. The actual main character is std::common_comparison_category, and she is serving comparison-category-commonality realness ✨.

🌸 NAME 🌸

std::common_comparison_category β€” std::common_comparison_category (the name said what it said, periodt πŸ’…).

✨ Synopsis ✨

Defined in header <compare>:

template< class... Ts >

struct common_comparison_category
{                                   (since C++20)
    using type = /* see below */ ;

};

The class template std::common_comparison_category provides an alias (as the member typedef type) for the strongest comparison category to which all of the template arguments Ts... can be converted. 🫢

In detail, the common comparison type of a list of n types T[0]...T[n-1] is defined as follows:

  • If any T[i] is not a comparison category type (std::partial_ordering, std::weak_ordering, std::strong_ordering), U is void.
  • Otherwise, if at least one T[i] is std::partial_ordering, U is std::partial_ordering.
  • Otherwise, if at least one T[i] is std::weak_ordering, U is std::weak_ordering.
  • Otherwise (if every T[i] is std::strong_ordering, or if the list is empty), U is std::strong_ordering.

🧩 Template parameters 🧩

  • ...Ts β€” a possibly empty list of types

πŸ› οΈ Helper template πŸ› οΈ

okay so there's also this helper alias, and it's giving "one-line convenience, no cap" 🌈:

template< class... Ts >
using common_comparison_category_t =
common_comparison_category<Ts...>::type;

✨ since C++20 ✨

πŸŽ€ Member types πŸŽ€

  • type β€” the strongest common comparison category (as defined above)

πŸ’… Possible implementation πŸ’…

here's a possible implementation, and no I did not invent it, it's right there in the original 😭:

namespace detail
{
    template<unsigned int>
    struct common_cmpcat_base     { using type = void; };
    template<>
    struct common_cmpcat_base<0u> { using type =
std::strong_ordering; };
    template<>
    struct common_cmpcat_base<2u> { using type =
std::partial_ordering; };
    template<>
    struct common_cmpcat_base<4u> { using type = std::weak_ordering;
};
    template<>
    struct common_cmpcat_base<6u> { using type =
std::partial_ordering; };
} // namespace detail

template<class...Ts>
struct common_comparison_category :
    detail::common_cmpcat_base<(0u | ... |
        (std::is_same_v<Ts, std::strong_ordering>  ? 0u :
         std::is_same_v<Ts, std::weak_ordering>    ? 4u :
         std::is_same_v<Ts, std::partial_ordering> ? 2u : 1u)
    )> {};

πŸ§ͺ Example πŸ§ͺ

This section is incomplete Reason: no example

πŸ‘€ See also πŸ‘€

  • strong_ordering (C++20) β€” the result type of 3-way comparison that supports all 6 operators and is substitutable (class)
  • weak_ordering (C++20) β€” the result type of 3-way comparison that supports all 6 operators and is not substitutable (class)
  • partial_ordering (C++20) β€” the result type of 3-way comparison that supports all 6 operators, is not substitutable, and allows incomparable values (class)

🏷️ Category 🏷️

  • Todo no example

and that's the whole page, babe πŸŽ€. this was std::common...son_category(3), brought to you by http://cppreference.com, version/date 2024.06.10 β€” go forth and compare responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from