Difference between revisions of "cpp/named req/BooleanTestable"
(+ BooleanTestable page. We should treat it as a DR to reflect the expectation of implementations.) |
m (+links.) |
||
(3 intermediate revisions by one user not shown) | |||
Line 2: | Line 2: | ||
{{cpp/named req/navbar}} | {{cpp/named req/navbar}} | ||
− | Specifies that an expression of such a type and value category is convertible to {{c/core|bool}}, and for which the logical operators for the type or two different {{named req/core|BooleanTestable}} types have the usual behavior (including short-circuiting). | + | Specifies that an expression of such a type and value category is convertible to {{c/core|bool}}, and for which the logical operators for the type or two different {{named req/core|BooleanTestable}} types have the usual behavior (including {{enwiki|Short-circuit evaluation|short-circuiting}}). |
===Requirements=== | ===Requirements=== | ||
Let {{c|e}} be an expression of a certain type and value category. The type and value category meet the {{named req/core|BooleanTestable}} requirements if: | Let {{c|e}} be an expression of a certain type and value category. The type and value category meet the {{named req/core|BooleanTestable}} requirements if: | ||
+ | * {{c|!e}} is well-formed and does not modify the object denoted by {{c|e}}, if any{{rev inl|since=c++11|, except that if {{c|e}} is an xvalue, the denoted object can be left in a valid but unspecified state}}. | ||
* Both {{c|e}} and {{c|!e}} are convertible to {{c/core|bool}} by both [[cpp/language/implicit_conversion|implicit conversion]] and {{ltt|cpp/language/static_cast}}. | * Both {{c|e}} and {{c|!e}} are convertible to {{c/core|bool}} by both [[cpp/language/implicit_conversion|implicit conversion]] and {{ltt|cpp/language/static_cast}}. | ||
* Conversions in both manners produce equal results and do not modify the source object, if any{{rev inl|since=c++11|, except that if {{c|e}} or {{c|!e}} is an xvalue, the source object can be left in a valid but unspecified state}}. | * Conversions in both manners produce equal results and do not modify the source object, if any{{rev inl|since=c++11|, except that if {{c|e}} or {{c|!e}} is an xvalue, the source object can be left in a valid but unspecified state}}. | ||
Line 15: | Line 16: | ||
The standard does not define a named requirement with this name. It was originally proposed in an early resolution of {{wg21|LWG2114}}, but was superseded by the exposition-only concept {{tti|{{lt|cpp/concepts/boolean-testable}}}} in the final resolution {{wg21|P2167R3}}. Because implementations generally expect the provided types to model {{tti|boolean-testable}} even in pre-C++20 modes, we intentedly treat P2167R3 as a defect report and transform the {{tti|boolean-testable}} to legacy named requirements. | The standard does not define a named requirement with this name. It was originally proposed in an early resolution of {{wg21|LWG2114}}, but was superseded by the exposition-only concept {{tti|{{lt|cpp/concepts/boolean-testable}}}} in the final resolution {{wg21|P2167R3}}. Because implementations generally expect the provided types to model {{tti|boolean-testable}} even in pre-C++20 modes, we intentedly treat P2167R3 as a defect report and transform the {{tti|boolean-testable}} to legacy named requirements. | ||
− | Examples of {{named req/core|BooleanTestable}} types (with any value category) include {{c/core|bool}}{{rev inl|since=c++11|, {{lc|std::true_type}}}}, {{c|std::bitset<N>::reference}}, and {{c|int*}}. | + | When the {{tt|&&}} and {{tt|{{!!}}}} operators are used with operands whose type and value category are {{named req/core|BooleanTestable}}, built-in versions are selected and short-circuit evaluation is performed. |
+ | |||
+ | {{rrev|since=c++20| | ||
+ | The type and value category of an expression {{c|e}} meet the {{named req/core|BooleanTestable}} requirements if and only if {{c|decltype((e))}} models {{tti|{{lt|cpp/concepts/boolean-testable}}}}.}} | ||
+ | |||
+ | Examples of {{named req/core|BooleanTestable}} types (with any value category) include {{c/core|bool}}{{rev inl|since=c++11|, {{lc|std::true_type}}}}, {{c/core|std::bitset<N>::}}{{ltt|cpp/utility/bitset/reference}}, and {{c/core|int*}}. | ||
===Defect reports=== | ===Defect reports=== | ||
Line 24: | Line 30: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/concepts/dsc boolean-testable}} | + | {{dsc inc|cpp/concepts/dsc boolean-testable}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|es|ja|zh}} | {{langlinks|es|ja|zh}} |
Latest revision as of 06:35, 9 September 2024
Specifies that an expression of such a type and value category is convertible to bool, and for which the logical operators for the type or two different BooleanTestable types have the usual behavior (including short-circuiting).
Contents |
[edit] Requirements
Let e be an expression of a certain type and value category. The type and value category meet the BooleanTestable requirements if:
- !e is well-formed and does not modify the object denoted by e, if any, except that if e is an xvalue, the denoted object can be left in a valid but unspecified state(since C++11).
- Both e and !e are convertible to bool by both implicit conversion and static_cast.
- Conversions in both manners produce equal results and do not modify the source object, if any, except that if e or !e is an xvalue, the source object can be left in a valid but unspecified state(since C++11).
- bool(!e) == !bool(e) holds.
- No viable non-member operator&& and operator|| is visible by argument-dependent lookup for the type of e or !e.
- If e or !e is of a class type, the class does not define any member operator&& or operator||.
[edit] Notes
The standard does not define a named requirement with this name. It was originally proposed in an early resolution of LWG2114, but was superseded by the exposition-only concept boolean-testable
in the final resolution P2167R3. Because implementations generally expect the provided types to model boolean-testable
even in pre-C++20 modes, we intentedly treat P2167R3 as a defect report and transform the boolean-testable
to legacy named requirements.
When the &&
and ||
operators are used with operands whose type and value category are BooleanTestable, built-in versions are selected and short-circuit evaluation is performed.
The type and value category of an expression e meet the BooleanTestable requirements if and only if decltype((e)) models |
(since C++20) |
Examples of BooleanTestable types (with any value category) include bool, std::true_type(since C++11), std::bitset<N>::reference, and int*.
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2114 (P2167R3) |
C++98 | convertibility to bool was too weak to reflect the expectation of implementations | requirements strengthened |
[edit] See also
(C++20) |
specifies that a type can be used in Boolean contexts (exposition-only concept*) |