Files
cppdraft_translate/cppdraft/concept/booleantestable.md
2025-10-25 03:02:53 +03:00

153 lines
6.4 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[concept.booleantestable]
# 18 Concepts library [[concepts]](./#concepts)
## 18.5 Comparison concepts [[concepts.compare]](concepts.compare#concept.booleantestable)
### 18.5.2 Boolean testability [concept.booleantestable]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L859)
The exposition-only [*boolean-testable*](#concept:boolean-testable "18.5.2Boolean testability[concept.booleantestable]") concept
specifies the requirements on expressions
that are convertible to bool and
for which the logical operators ([[expr.log.and]](expr.log.and "7.6.14Logical AND operator"), [[expr.log.or]](expr.log.or "7.6.15Logical OR operator"), [[expr.unary.op]](expr.unary.op "7.6.2.2Unary operators"))
have the conventional semantics[.](#1.sentence-1)
[🔗](#concept:boolean-testable-impl)
`template<class T>
concept [boolean-testable-impl](#concept:boolean-testable-impl "18.5.2Boolean testability[concept.booleantestable]") = [convertible_to](concept.convertible#concept:convertible_to "18.4.4Concept convertible_­to[concept.convertible]")<T, bool>; // exposition only
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L871)
Let e be an expression such thatdecltype((e)) is T[.](#2.sentence-1)
T models [*boolean-testable-impl*](#concept:boolean-testable-impl "18.5.2Boolean testability[concept.booleantestable]") only if
- [(2.1)](#2.1)
either remove_cvref_t<T> is not a class type, or
a search for the names operator&& and operator|| in the scope of remove_cvref_t<T> finds nothing; and
- [(2.2)](#2.2)
argument-dependent lookup ([[basic.lookup.argdep]](basic.lookup.argdep "6.5.4Argument-dependent name lookup"))
for the names operator&& and operator|| with T as the only argument type
finds no disqualifying declaration (defined below)[.](#2.sentence-2)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L889)
A [*disqualifying parameter*](#def:parameter,disqualifying "18.5.2Boolean testability[concept.booleantestable]") is a function parameter whose declared type P
- [(3.1)](#3.1)
is not dependent on a template parameter, and
there exists an implicit conversion sequence ([[over.best.ics]](over.best.ics "12.2.4.2Implicit conversion sequences"))
from e to P; or
- [(3.2)](#3.2)
is dependent on one or more template parameters, and either
* [(3.2.1)](#3.2.1)
P contains no template parameter that
participates in template argument deduction ([[temp.deduct.type]](temp.deduct.type "13.10.3.6Deducing template arguments from a type")), or
* [(3.2.2)](#3.2.2)
template argument deduction
using the rules for deducing template arguments
in a function call ([[temp.deduct.call]](temp.deduct.call "13.10.3.2Deducing template arguments from a function call")) ande as the argument succeeds[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L912)
A [*key parameter*](#def:parameter,key "18.5.2Boolean testability[concept.booleantestable]") of a function template D is a function parameter of type cv X or reference thereto,
where X names a specialization of a class template that
has the same innermost enclosing non-inline namespace as D, andX contains at least one template parameter that
participates in template argument deduction[.](#4.sentence-1)
[*Example [1](#example-1)*:
Innamespace Z {template<class> struct C {}; template<class T>void operator&&(C<T> x, T y); template<class T>void operator||(C<type_identity_t<T>> x, T y);} the declaration of Z::operator&& contains one key parameter, C<T> x, and
the declaration of Z::operator|| contains no key parameters[.](#4.sentence-2)
— *end example*]
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L937)
A [*disqualifying declaration*](#def:declaration,disqualifying "18.5.2Boolean testability[concept.booleantestable]") is
- [(5.1)](#5.1)
a (non-template) function declaration that
contains at least one disqualifying parameter; or
- [(5.2)](#5.2)
a function template declaration that
contains at least one disqualifying parameter, where
* [(5.2.1)](#5.2.1)
at least one disqualifying parameter is a key parameter; or
* [(5.2.2)](#5.2.2)
the declaration contains no key parameters; or
* [(5.2.3)](#5.2.3)
the declaration declares a function template
to which no name is bound ([[dcl.meaning]](dcl.meaning "9.3.4Meaning of declarators"))[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L955)
[*Note [1](#note-1)*:
The intention is to ensure that
given two types T1 and T2 that each model [*boolean-testable-impl*](#concept:boolean-testable-impl "18.5.2Boolean testability[concept.booleantestable]"),
the && and || operators within the expressionsdeclval<T1>() && declval<T2>() anddeclval<T1>() || declval<T2>() resolve to the corresponding built-in operators[.](#6.sentence-1)
— *end note*]
[🔗](#concept:boolean-testable)
`template<class T>
concept [boolean-testable](#concept:boolean-testable "18.5.2Boolean testability[concept.booleantestable]") = // exposition only
[boolean-testable-impl](#concept:boolean-testable-impl "18.5.2Boolean testability[concept.booleantestable]")<T> && requires(T&& t) {
{ !std::forward<T>(t) } -> [boolean-testable-impl](#concept:boolean-testable-impl "18.5.2Boolean testability[concept.booleantestable]");
};
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L974)
Let e be an expression such thatdecltype((e)) is T[.](#7.sentence-1)
T models [*boolean-testable*](#concept:boolean-testable "18.5.2Boolean testability[concept.booleantestable]") only ifbool(e) == !bool(!e)[.](#7.sentence-2)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/concepts.tex#L980)
[*Example [2](#example-2)*:
The typesbool,true_type ([[meta.type.synop]](meta.type.synop "21.3.3Header <type_­traits> synopsis")),int*, andbitset<N>::reference ([[template.bitset]](template.bitset "22.9.2Class template bitset"))
model [*boolean-testable*](#concept:boolean-testable "18.5.2Boolean testability[concept.booleantestable]")[.](#8.sentence-1)
— *end example*]