std::boolean (C++20 起)(C++23 前)
在标头 <concepts> 定义
|
||
template<class B> concept boolean = |
(C++20 起) (C++23 前) (1) |
|
- (1) — 仅当满足以下情况时,
B
实现boolean
:- bool(b1) == !bool(!b1)。
- (b1 && b2)、(b1 && bool(b2)) 和 (bool(b1) && b2) 都等于 (bool(b1) && bool(b2))。(2)
- (b1 || b2)、(b1 || bool(b2)) 和 (bool(b1) || b2) 都等于 (bool(b1) || bool(b2))。(2)
- bool(b1 == b2)、bool(b1 == bool(b2)) 和 bool(bool(b1) == b2) 都等于 (bool(b1) == bool(b2))。
- bool(b1 != b2)、bool(b1 != bool(b2)) 和 bool(bool(b1) != b2) 都等于 (bool(b1) != bool(b2))。
- (2) — 并且进行相同的短路求值。
[编辑] 注解
boolean
类型的例子:
�� boolean
类型的例子:
[编辑] 示例
#include <bitset> // std::bitset #include <concepts> // std::boolean #include <cstddef> // nullptr_t #include <memory> // std::weak_ptr #include <string> // std::string #include <type_traits> // std::true_type static_assert(std::boolean<bool>); static_assert(std::boolean<std::true_type>); static_assert(std::boolean<std::bitset<42>::reference>); static_assert(std::boolean<int>); static_assert(!std::boolean<void*>); static_assert(!std::boolean<nullptr_t>); static_assert(!std::boolean<std::weak_ptr<bool>>); static_assert(!std::boolean<std::string>); struct s0 {[[nodiscard]] constexpr /* implicit */ operator bool() const noexcept;}; struct s1 {[[nodiscard]] constexpr explicit operator bool() const noexcept;}; static_assert(std::boolean<s0>); static_assert(!std::boolean<s1>); int main() noexcept {};
[编辑] 引用
- C++20 标准(ISO/IEC 14882:2020):
- 18.5.2 Concept
boolean
[concept.boolean]
- 18.5.2 Concept