Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/concepts/boolean-testable"

From cppreference.com
< cpp‎ | concepts
m (Added Spanish link)
m (fmt.)
 
(14 intermediate revisions by 9 users not shown)
Line 2: Line 2:
 
{{cpp/concepts/navbar}}
 
{{cpp/concepts/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl|since=c++20|1=
+
{{dcla|anchor=no|num=1|since=c++20|expos=yes|1=
template<class B>
+
template< class B >
concept __boolean_testable_impl =               // exposition only
+
concept __boolean_testable_impl = std::convertible_to<B, bool>;
    std::convertible_to<B, bool>;
+
 
}}
 
}}
{{dcl|since=c++20|1=
+
{{dcla|anchor=no|num=2|since=c++20|expos=yes|1=
template<class B>
+
template< class B >
concept boolean-testable =                       // exposition only
+
concept boolean-testable =
 
     __boolean_testable_impl<B> &&
 
     __boolean_testable_impl<B> &&
 
     requires (B&& b) {
 
     requires (B&& b) {
         { !std::forward<B>(b) } -> __boolean_testable_impl
+
         { !std::forward<B>(b) } -> __boolean_testable_impl;
 
     };
 
     };
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
The exposition-only concept {{tt|''boolean-testable''}} specifies the requirements for expressions that are convertible to {{c|bool}} and for which the logical operators have the usual behavior (including short-circuiting), even for two different {{tt|''boolean-testable''}} types.
+
The exposition-only concept {{tti|boolean-testable}} specifies the requirements for expressions that are convertible to {{c/core|bool}} and for which the logical operators have the usual behavior (including {{enwiki|Short-circuit evaluation|short-circuiting}}), even for two different {{tti|boolean-testable}} types.
  
Formally, to model the exposition-only concept {{tt|__boolean_testable_impl}}, the type must not define any member {{tt|operator&&}} and {{tt|operator{{!!}}}}, and no viable non-member {{tt|operator&&}} and {{tt|operator{{!!}}}} may be visible by [[cpp/language/adl|argument-dependent lookup]]. Additionally, given an expression {{tt|e}} such that {{c|decltype((e))}} is {{tt|B}}, {{tt|''boolean-testable''}} is modeled only if {{c|bool(e) {{==}} !bool(!e)}}.
+
Formally, to model the exposition-only concept {{tti|__boolean_testable_impl}}, the type must not define any member {{c/core|operator&&}} and {{c/core|operator{{!!}}}}, and no viable non-member {{c/core|operator&&}} and {{c/core|operator{{!!}}}} may be visible by [[cpp/language/adl|argument-dependent lookup]]. Additionally, given an expression {{tt|e}} such that {{c|decltype((e))}} is {{tt|B}}, {{tti|boolean-testable}} is modeled only if {{c|1=bool(e) == !bool(!e)}}.
  
 
{{cpp/concepts/equality preservation}}
 
{{cpp/concepts/equality preservation}}
  
=== Notes===
+
===Notes===
Examples of {{tt|''boolean-testable''}} types include {{c|bool}}, {{c|std::true_type}}, and {{c|std::bitset<N>::reference}}, and {{c|int*}}.
+
Examples of {{tti|boolean-testable}} types include {{c/core|bool}}, {{lc|std::true_type}}, {{c/core|std::bitset<N>::}}{{ltt|cpp/utility/bitset/reference}}, and {{c/core|int*}}.
 +
 
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=18.5.2|title=Boolean testability|id=concept.booleantestable}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=18.5.2|title=Boolean testability|id=concept.booleantestable}}
 +
{{ref std end}}
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Latest revision as of 05:15, 9 September 2024

template< class B >
concept __boolean_testable_impl = std::convertible_to<B, bool>;
(1) (since C++20)
(exposition only*)
template< class B >

concept boolean-testable =
    __boolean_testable_impl<B> &&
    requires (B&& b) {
        { !std::forward<B>(b) } -> __boolean_testable_impl;

    };
(2) (since C++20)
(exposition only*)

The exposition-only concept boolean-testable specifies the requirements for expressions that are convertible to bool and for which the logical operators have the usual behavior (including short-circuiting), even for two different boolean-testable types.

Formally, to model the exposition-only concept __boolean_testable_impl, the type must not define any member operator&& and operator||, and no viable non-member operator&& and operator|| may be visible by argument-dependent lookup. Additionally, given an expression e such that decltype((e)) is B, boolean-testable is modeled only if bool(e) == !bool(!e).

[edit] Equality preservation

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

[edit] Notes

Examples of boolean-testable types include bool, std::true_type, std::bitset<N>::reference, and int*.

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 18.5.2 Boolean testability [concept.booleantestable]
  • C++20 standard (ISO/IEC 14882:2020):
  • 18.5.2 Boolean testability [concept.booleantestable]