Namespaces
Variants
Views
Actions

std::boolean

From cppreference.com
< cpp‎ | concepts
Revision as of 16:42, 28 August 2019 by Fruderica (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <concepts>
template<class B>

  concept boolean =
    std::movable<std::remove_cvref_t<B>> &&
    requires(const std::remove_reference_t<B>& b1,
             const std::remove_reference_t<B>& b2, const bool a) {
      { b1 } -> std::convertible_to<bool>;
      { !b1 } -> std::convertible_to<bool>;
      { b1 && b2 } -> std::same_as<bool>;
      { b1 &&  a } -> std::same_as<bool>;
      {  a && b2 } -> std::same_as<bool>;
      { b1 || b2 } -> std::same_as<bool>;
      { b1 ||  a } -> std::same_as<bool>;
      {  a || b2 } -> std::same_as<bool>;
      { b1 == b2 } -> std::convertible_to<bool>;
      { b1 ==  a } -> std::convertible_to<bool>;
      {  a == b2 } -> std::convertible_to<bool>;
      { b1 != b2 } -> std::convertible_to<bool>;
      { b1 !=  a } -> std::convertible_to<bool>;
      {  a != b2 } -> std::convertible_to<bool>;

    };
(since C++20)

The concept boolean<B> specifies the requirements for a type usable in Boolean contexts. For boolean to be satisfied, the logical operators must have the usual behavior (including short-circuiting). More precisely, given

boolean<B> is satisfied only if:

  • bool(b1) == !bool(!b1)
  • b1 && b2, b1 && bool(b2) and bool(b1) && b2 are all equal to bool(b1) && bool(b2) and have the same short-circuit evaluation;
  • b1 || b2, b1 || bool(b2) and bool(b1) || b2 are all equal to bool(b1) || bool(b2) and have the same short-circuit evaluation;
  • bool(b1 == b2), bool(b1 == bool(b2)), and bool(bool(b1) == b2) are all equal to (bool(b1) == bool(b2));
  • bool(b1 != b2), bool(b1 != bool(b2)), and bool(bool(b1) != b2) are all equal to (bool(b1) != bool(b2)).

Equality preservation

Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise).

Implicit expression variations

A requires expression that uses an expression that is non-modifying for some constant lvalue operand also requires implicit expression variations.

Notes

Examples of boolean types include bool, std::true_type, and std::bitset<N>::reference. Pointers are not boolean types.