标准库头文件 <concepts> (C++20)
来自 cppreference.com
此头文件是 概念 库的一部分。
概念 | |
核心语言概念 | |
(C++20) |
指定一个类型与另一个类型相同 (概念) |
(C++20) |
指定一个类型派生自另一个类型 (概念) |
(C++20) |
指定一个类型隐式可转换为另一个类型 (概念) |
(C++20) |
指定两个类型共享一个公共引用类型 (概念) |
(C++20) |
指定两个类型共享一个公共类型 (概念) |
(C++20) |
指定一个类型是整数类型 (概念) |
(C++20) |
指定一个类型是有符号的整数类型 (概念) |
(C++20) |
指定一个类型是无符号的整数类型 (概念) |
(C++20) |
指定一个类型是浮点类型 (概念) |
(C++20) |
指定一个类型可以从另一个类型赋值 (概念) |
(C++20) |
指定一个类型可以交换或两个类型可以相互交换 (概念) |
(C++20) |
指定该类型对象可以被销毁 (概念) |
(C++20) |
指定该类型变量可以从一组参数类型构造或绑定 (概念) |
(C++20) |
指定该类型对象可以默认构造 (概念) |
(C++20) |
指定该类型对象可以移动构造 (概念) |
(C++20) |
指定该类型对象可以复制构造和移动构造 (概念) |
比较概念 | |
指定运算符 == 是一个等价关系 (概念) | |
指定该类型上的比较运算符产生一个全序 (概念) | |
对象概念 | |
(C++20) |
指定该类型对象可以移动和交换 (概念) |
(C++20) |
指定该类型对象可以复制、移动和交换 (概念) |
(C++20) |
指定该类型对象可以复制、移动、交换和默认构造 (概念) |
(C++20) |
指定一个类型是正则的,也就是说,它既是 semiregular 又是 equality_comparable (概念) |
可调用概念 | |
(C++20) |
指定一个可调用类型可以用给定的一组参数类型调用 (概念) |
(C++20) |
指定一个可调用类型是一个布尔谓词 (概念) |
(C++20) |
指定一个可调用类型是一个二元关系 (概念) |
(C++20) |
指定一个 relation 强加了一个等价关系(概念) |
(C++20) |
指定一个 relation 强加了一个严格弱序(概念) |
自定义点对象 | |
(C++20) |
交换两个对象的值 (自定义点对象) |
[编辑] 提要
// all freestanding namespace std { // language-related concepts // concept same_as template<class T, class U> concept same_as = /* see description */; // concept derived_from template<class Derived, class Base> concept derived_from = /* see description */; // concept convertible_to template<class From, class To> concept convertible_to = /* see description */; // concept common_reference_with template<class T, class U> concept common_reference_with = /* see description */; // concept common_with template<class T, class U> concept common_with = /* see description */; // arithmetic concepts template<class T> concept integral = /* see description */; template<class T> concept signed_integral = /* see description */; template<class T> concept unsigned_integral = /* see description */; template<class T> concept floating_point = /* see description */; // concept assignable_from template<class LHS, class RHS> concept assignable_from = /* see description */; // concept swappable namespace ranges { inline namespace /* unspecified */ { inline constexpr /* unspecified */ swap = /* unspecified */; } } template<class T> concept swappable = /* see description */; template<class T, class U> concept swappable_with = /* see description */; // concept destructible template<class T> concept destructible = /* see description */; // concept constructible_from template<class T, class... Args> concept constructible_from = /* see description */; // concept default_initializable template<class T> concept default_initializable = /* see description */; // concept move_constructible template<class T> concept move_constructible = /* see description */; // concept copy_constructible template<class T> concept copy_constructible = /* see description */; // comparison concepts // concept equality_comparable template<class T> concept equality_comparable = /* see description */; template<class T, class U> concept equality_comparable_with = /* see description */; // concept totally_ordered template<class T> concept totally_ordered = /* see description */; template<class T, class U> concept totally_ordered_with = /* see description */; // object concepts template<class T> concept movable = /* see description */; template<class T> concept copyable = /* see description */; template<class T> concept semiregular = /* see description */; template<class T> concept regular = /* see description */; // callable concepts // concept invocable template<class F, class... Args> concept invocable = /* see description */; // concept regular_invocable template<class F, class... Args> concept regular_invocable = /* see description */; // concept predicate template<class F, class... Args> concept predicate = /* see description */; // concept relation template<class R, class T, class U> concept relation = /* see description */; // concept equivalence_relation template<class R, class T, class U> concept equivalence_relation = /* see description */; // concept strict_weak_order template<class R, class T, class U> concept strict_weak_order = /* see description */; }
[编辑] 辅助概念 boolean-testable
template<class T> concept /*boolean-testable-impl*/ = convertible_to<T, bool>; // exposition only; template<class T> concept boolean-testable = // exposition only /*boolean-testable-impl*/<T> && requires(T&& t) { { !std::forward<T>(t) } -> /*boolean-testable-impl*/; };
[编辑] 概念 same_as
template<class T, class U> concept /*same-as-impl*/ = is_same_v<T, U>; // exposition only template<class T, class U> concept same_as = /*same-as-impl*/<T, U> && /*same-as-impl*/<U, T>;
[编辑] 概念 derived_from
template<class Derived, class Base> concept derived_from = is_base_of_v<Base, Derived> && is_convertible_v<const volatile Derived*, const volatile Base*>;
[编辑] 概念 convertible_to
template<class From, class To> concept convertible_to = is_convertible_v<From, To> && requires { static_cast<To>(declval<From>()); };
[编辑] 概念 common_reference_with
template<class T, class U> concept common_reference_with = same_as<common_reference_t<T, U>, common_reference_t<U, T>> && convertible_to<T, common_reference_t<T, U>> && convertible_to<U, common_reference_t<T, U>>;
[编辑] 概念 common_with
template<class T, class U> concept common_with = same_as<common_type_t<T, U>, common_type_t<U, T>> && requires { static_cast<common_type_t<T, U>>(declval<T>()); static_cast<common_type_t<T, U>>(declval<U>()); } && common_reference_with<add_lvalue_reference_t<const T>, add_lvalue_reference_t<const U>> && common_reference_with< add_lvalue_reference_t<common_type_t<T, U>>, common_reference_t<add_lvalue_reference_t<const T>, add_lvalue_reference_t<const U>>>;
[编辑] 概念 integral
template<class T> concept integral = is_integral_v<T>;
[编辑] 概念 signed_integral
template<class T> concept signed_integral = integral<T> && is_signed_v<T>;
[编辑] 概念 unsigned_integral
template<class T> concept unsigned_integral = integral<T> && !signed_integral<T>;
[编辑] 概念 floating_point
template<class T> concept floating_point = is_floating_point_v<T>;
[编辑] 概念 assignable_from
template<class LHS, class RHS> concept assignable_from = is_lvalue_reference_v<LHS> && common_reference_with<const remove_reference_t<LHS>&, const remove_reference_t<RHS>&> && requires(LHS lhs, RHS&& rhs) { { lhs = std::forward<RHS>(rhs) } -> same_as<LHS>; };
[编辑] 概念 swappable
template<class T> concept swappable = requires(T& a, T& b) { ranges::swap(a, b); };
[编辑] 概念 swappable_with
template<class T, class U> concept swappable_with = common_reference_with<T, U> && requires(T&& t, U&& u) { ranges::swap(std::forward<T>(t), std::forward<T>(t)); ranges::swap(std::forward<U>(u), std::forward<U>(u)); ranges::swap(std::forward<T>(t), std::forward<U>(u)); ranges::swap(std::forward<U>(u), std::forward<T>(t)); };
[编辑] 概念 destructible
template<class T> concept destructible = is_nothrow_destructible_v<T>;
[编辑] 概念 constructible_from
template<class T, class... Args> concept constructible_from = destructible<T> && is_constructible_v<T, Args...>;
[编辑] 概念 default_initializable
template<class T> constexpr bool /*is-default-initializable*/ = /* see description */; // exposition only template<class T> concept default_initializable = constructible_from<T> && requires { T{}; } && /*is-default-initializable*/<T>;
[编辑] 概念 move_constructible
template<class T> concept move_constructible = constructible_from<T, T> && convertible_to<T, T>;
[编辑] 概念 copy_constructible
template<class T> concept copy_constructible = move_constructible<T> && constructible_from<T, T&> && convertible_to<T&, T> && constructible_from<T, const T&> && convertible_to<const T&, T> && constructible_from<T, const T> && convertible_to<const T, T>;
[编辑] 概念 equality_comparable
template<class T, class U> concept /*weakly-equality-comparable-with*/ = // exposition only requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) { { t == u } -> boolean-testable; { t != u } -> boolean-testable; { u == t } -> boolean-testable; { u != t } -> boolean-testable; }; template<class T> concept equality_comparable = /*weakly-equality-comparable-with*/<T, T>;
[编辑] 概念 equality_comparable_with
template<class T, class U, class C = common_reference_t<const T&, const U&>> concept /*comparison-common-type-with-impl*/ = // exposition only same_as<common_reference_t<const T&, const U&>, common_reference_t<const U&, const T&>> && requires { requires convertible_to<const T&, const C&> || convertible_to<T, const C&>; requires convertible_to<const U&, const C&> || convertible_to<U, const C&>; }; template<class T, class U> concept /*comparison-common-type-with*/ = // exposition only /*comparison-common-type-with-impl*/<remove_cvref_t<T>, remove_cvref_t<U>>; template<class T, class U> concept equality_comparable_with = equality_comparable<T> && equality_comparable<U> && /*comparison-common-type-with*/<T, U> && equality_comparable< common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>> && /*weakly-equality-comparable-with*/<T, U>;
[编辑] 辅助概念 partially-ordered-with
定义在头文件 <compare> 中
template<class T, class U> concept /*partially-ordered-with*/ = // exposition only requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) { { t < u } -> boolean-testable; { t > u } -> boolean-testable; { t <= u } -> boolean-testable; { t >= u } -> boolean-testable; { u < t } -> boolean-testable; { u > t } -> boolean-testable; { u <= t } -> boolean-testable; { u >= t } -> boolean-testable; };
[编辑] 概念 totally_ordered
template<class T> concept totally_ordered = equality_comparable<T> && /*partially-ordered-with*/<T, T>;
[编辑] 概念 totally_ordered_with
template<class T, class U> concept totally_ordered_with = totally_ordered<T> && totally_ordered<U> && equality_comparable_with<T, U> && totally_ordered< common_reference_t<const remove_reference_t<T>&, const remove_reference_t<U>&>> && /*partially-ordered-with*/<T, U>;
[编辑] 概念 movable
template<class T> concept movable = is_object_v<T> && move_constructible<T> && assignable_from<T&, T> && swappable<T>;
[编辑] 概念 copyable
template<class T> concept copyable = copy_constructible<T> && movable<T> && assignable_from<T&, T&> && assignable_from<T&, const T&> && assignable_from<T&, const T>;
[编辑] 概念 semiregular
template<class T> concept semiregular = copyable<T> && default_initializable<T>;
[编辑] 概念 regular
template<class T> concept regular = semiregular<T> && equality_comparable<T>;
[编辑] 概念 invocable
template<class F, class... Args> concept invocable = requires(F&& f, Args&&... args) { invoke(std::forward<F>(f), std::forward<Args>(args)...); // not required to be equality-preserving };
[编辑] 概念 regular_invocable
template<class F, class... Args> concept regular_invocable = invocable<F, Args...>;
[编辑] 概念 predicate
template<class F, class... Args> concept predicate = regular_invocable<F, Args...> && boolean-testable<invoke_result_t<F, Args...>>;
[编辑] 概念 relation
template<class R, class T, class U> concept relation = predicate<R, T, T> && predicate<R, U, U> && predicate<R, T, U> && predicate<R, U, T>;
[编辑] 概念 equivalence_relation
template<class R, class T, class U> concept equivalence_relation = relation<R, T, U>;
[编辑] 概念 strict_weak_order
template<class R, class T, class U> concept strict_weak_order = relation<R, T, U>;