std::disjunction(3)

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

🌸 NAME 🌸

std::disjunction β€” std::disjunction. (Yes bestie, the name is literally the name. Iconic.) This is the C++ Standard Libary manual page, section 3, and today's main character is std::disjunction. πŸ’…


✨ Synopsis ✨

Defined in header <type_traits>
template< class... B >           (since C++17)
struct disjunction;

Okay so std::disjunction forms the logical disjunction of the type traits B..., effectively performing a logical OR on the sequence of traits. It's giving compile-time OR energy, and honestly? Obsessed. ✨

The specialization std::disjunction<B1, ..., BN> has a public and unambiguous base that is:

  • if sizeof...(B) == 0, std::false_type; otherwise
  • the first type Bi in B1, ..., BN for which bool(Bi::value) == true, or BN if there is no such type.

The member names of the base class, other than disjunction and operator=, are not hidden and are unambiguously available in disjunction.

Disjunction is short-circuiting: if there is a template type argument Bi with bool(Bi::value) != false, then instantiating disjunction<B1, ..., BN>::value does not require the instantiation of Bj::value for j > i.

Bestie, warning moment: if the program adds specializations for std::disjunction or std::disjunction_v, the behavior is undefined. Not cute, not slay, do not do it. πŸ’€

🧩 Template parameters 🧩

  • B... β€” every template argument Bi for which Bi::value is instantiated must be usable as a base class and define member value that is convertible to bool.

πŸŽ€ Helper variable template (since C++17) πŸŽ€

There's also this helper variable template, and it's so handy:

template< class... B >
inline constexpr bool disjunction_v = disjunction<B...>::value;

πŸ› οΈ Possible implementation πŸ› οΈ

Here's the vibe in code form, bestie:

template<class...> struct disjunction : std::false_type {};
template<class B1> struct disjunction<B1> : B1 {};
template<class B1, class... Bn>
struct disjunction<B1, Bn...>
    : std::conditional_t<bool(B1::value), B1, disjunction<Bn...>>
{};

πŸ“ Notes πŸ“

A specialization of disjunction does not necessarily inherit from either std::true_type or std::false_type: it simply inherits from the first B whose ::value, explicitly converted to bool, is true, or from the very last B when all of them convert to false. For example, std::disjunction<std::integral_constant<int, 2>, std::integral_constant<int, 4>>::value is 2.

The short-circuit instantiation differentiates disjunction from fold expressions: a fold expression like (... || Bs::value) instantiates every B in Bs, while std::disjunction_v<Bs...> stops instantiation once the value can be determined. This is particularly useful if the later type is expensive to instantiate or can cause a hard error when instantiated with the wrong type.

πŸ“Š Feature-test macro πŸ“Š

  • __cpp_lib_logical_traits β€” value 201510L, std (C++17), feature Logical operator type traits.

πŸ’» Example πŸ’»

// Run this code

#include <cstdint>
#include <string>
#include <type_traits>

// values_equal<a, b, T>::value is true if and only if a == b.
template<auto V1, decltype(V1) V2, typename T>
struct values_equal : std::bool_constant<V1 == V2>
{
    using type = T;
};

// default_type<T>::value is always true
template<typename T>
struct default_type : std::true_type
{
    using type = T;
};

// Now we can use disjunction like a switch statement:
template<int I>
using int_of_size = typename std::disjunction< //
    values_equal<I, 1, std::int8_t>,           //
    values_equal<I, 2, std::int16_t>,          //
    values_equal<I, 4, std::int32_t>,          //
    values_equal<I, 8, std::int64_t>,          //
    default_type<void>                         // must be last!
    >::type;

static_assert(sizeof(int_of_size<1>) == 1);
static_assert(sizeof(int_of_size<2>) == 2);
static_assert(sizeof(int_of_size<4>) == 4);
static_assert(sizeof(int_of_size<8>) == 8);
static_assert(std::is_same_v<int_of_size<13>, void>);

// checking if Foo is constructible from double will cause a hard error
struct Foo
{
    template<class T>
    struct sfinae_unfriendly_check { static_assert(!std::is_same_v<T, double>); };

    template<class T>
    Foo(T, sfinae_unfriendly_check<T> = {});
};

template<class... Ts>
struct first_constructible
{
    template<class T, class...Args>
    struct is_constructible_x : std::is_constructible<T, Args...>
    {
        using type = T;
    };
    struct fallback
    {
        static constexpr bool value = true;
        using type = void; // type to return if nothing is found
    };

    template<class... Args>
    using with = typename std::disjunction<is_constructible_x<Ts, Args...>...,
                                           fallback>::type;
};

// OK, is_constructible<Foo, double> not instantiated
static_assert(std::is_same_v<first_constructible<std::string, int, Foo>::with<double>,
                             int>);

static_assert(std::is_same_v<first_constructible<std::string, int>::with<>, std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<const char*>,
                             std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<void*>, void>);

int main() {}

πŸ‘€ See also πŸ‘€

  • negation β€” logical NOT metafunction (C++17) (class template).
  • conjunction β€” variadic logical AND metafunction (C++17) (class template).

Source/version/date tea β˜•: this page is from http://cppreference.com, dated 2024.06.10, and the manual page is std::disjunction(3). Bye bestie, go short-circuit something cute! πŸ’–πŸ¦‹

πŸ“¦ where this page comes from