标准库标头 <concepts>

来自cppreference.com
< cpp‎ | header


 
 
标准库头
通用工具
<any> (C++17)
<bitset>
<bit> (C++20)
<charconv> (C++17)
<expected> (C++23)
<format> (C++20)
<functional>
<optional> (C++17)
<stdbit.h> (C++26)
<tuple> (C++11)
<typeindex> (C++11)
<utility>
<variant> (C++17)
容器
<array> (C++11)
<deque>
<flat_map> (C++23)
<flat_set> (C++23)
<forward_list> (C++11)
<hive> (C++26)
<inplace_vector> (C++26)   
<list>
<map>
<mdspan> (C++23)
<queue>
<set>
<span> (C++20)
<stack>
<unordered_map> (C++11)
<unordered_set> (C++11)
<vector>
迭代器
<iterator>
范围
<generator> (C++23)
<ranges> (C++20)
 

此头文件是概念库的一部分。

目录

概念

核心语言概念
(C++20)
指定一个类型与另一类型相同
(概念) [编辑]
指定一个类型派生自另一类型
(概念) [编辑]
指定一个类型能隐式转换成另一类型
(概念) [编辑]
指定两个类型共有一个公共引用类型
(概念) [编辑]
指定两个类型共有一个公共类型
(概念) [编辑]
(C++20)
指定类型为整数类型
(概念) [编辑]
指定类型为有符号整数类型
(概念) [编辑]
指定类型为无符号整数类型
(概念) [编辑]
指定类型为浮点数类型
(概念) [编辑]
指定一个类型能从另一类型赋值
(概念) [编辑]
指定一个类型能进行交换,或两个类型能彼此交换
(概念) [编辑]
指定能销毁该类型的对象
(概念) [编辑]
指定该类型的变量能从一组实参类型进行构造,或绑定到一组实参类型
(概念) [编辑]
指定一个类型的对象能默认构造
(概念) [编辑]
指定能移动构造一个类型的对象
(概念) [编辑]
指定能复制构造和移动构造一个类型的对象
(概念) [编辑]
比较概念
指定运算符 == 为等价关系
(概念) [编辑]
指定比较运算符在该类型上产生全序
(概念) [编辑]
对象概念
(C++20)
指定能移动及交换一个类型的对象
(概念) [编辑]
(C++20)
指定能复制、移动及交换一个类型的对象
(概念) [编辑]
指定能赋值、移动、交换及默认构造一个类型的对象
(概念) [编辑]
(C++20)
指定类型为正则,即它既 semiregularequality_comparable
(概念) [编辑]
可调用概念
指定能以给定的一组实参类型调用的可调用类型
(概念) [编辑]
(C++20)
指定可调用类型为布尔谓词
(概念) [编辑]
(C++20)
指定可调用类型为二元关系
(概念) [编辑]
指定 relation 施加等价关系
(概念) [编辑]
指定 relation 施加的是严格弱序
(概念) [编辑]

定制点对象

交换两个对象的值
(定制点对象) [编辑]


[编辑] 概要

// 全为独立
namespace std {
  // 语言相关概念
  // 概念 same_as
  template<class T, class U>
  concept same_as = /* 见描述 */;
 
  // 概念 derived_from
  template<class Derived, class Base>
  concept derived_from = /* 见描述 */;
 
  // 概念 convertible_to
  template<class From, class To>
  concept convertible_to = /* 见描述 */;
 
  // 概念 common_reference_with
  template<class T, class U>
  concept common_reference_with = /* 见描述 */;
 
  // 概念 common_with
  template<class T, class U>
  concept common_with = /* 见描述 */;
 
  // 算术概念
  template<class T>
  concept integral = /* 见描述 */;
  template<class T>
  concept signed_integral = /* 见描述 */;
  template<class T>
  concept unsigned_integral = /* 见描述 */;
  template<class T>
  concept floating_point = /* 见描述 */;
 
  // 概念 assignable_from
  template<class LHS, class RHS>
  concept assignable_from = /* 见描述 */;
 
  // 概念 swappable
  namespace ranges {
    inline namespace /* 未指明 */ {
      inline constexpr /* 未指明 */ swap = /* 未指明 */;
    }
  }
  template<class T>
  concept swappable = /* 见描述 */;
  template<class T, class U>
  concept swappable_with = /* 见描述 */;
 
  // 概念 destructible
  template<class T>
  concept destructible = /* 见描述 */;
 
  // 概念 constructible_from
  template<class T, class... Args>
  concept constructible_from = /* 见描述 */;
 
  // 概念 default_initializable
  template<class T>
  concept default_initializable = /* 见描述 */;
 
  // 概念 move_constructible
  template<class T>
  concept move_constructible = /* 见描述 */;
 
  // 概念 copy_constructible
  template<class T>
  concept copy_constructible = /* 见描述 */;
 
  // 比较概念
  // 概念 equality_comparable
  template<class T>
  concept equality_comparable = /* 见描述 */;
  template<class T, class U>
  concept equality_comparable_with = /* 见描述 */;
 
  // 概念 totally_ordered
  template<class T>
  concept totally_ordered = /* 见描述 */;
  template<class T, class U>
  concept totally_ordered_with = /* 见描述 */;
 
  // 对象概念
  template<class T>
  concept movable = /* 见描述 */;
  template<class T>
  concept copyable = /* 见描述 */;
  template<class T>
  concept semiregular = /* 见描述 */;
  template<class T>
  concept regular = /* 见描述 */;
 
  // 可调用概念
  // 概念 invocable
  template<class F, class... Args>
  concept invocable = /* 见描述 */;
 
  // 概念 regular_invocable
  template<class F, class... Args>
  concept regular_invocable = /* 见描述 */;
 
  // 概念 predicate
  template<class F, class... Args>
  concept predicate = /* 见描述 */;
 
  // 概念 relation
  template<class R, class T, class U>
  concept relation = /* 见描述 */;
 
  // 概念 equivalence_relation
  template<class R, class T, class U>
  concept equivalence_relation = /* 见描述 */;
 
  // 概念 strict_weak_order
  template<class R, class T, class U>
  concept strict_weak_order = /* 见描述 */;
}

[编辑] 辅助概念 boolean-testable

template<class T>
concept /*boolean-testable-impl*/ = convertible_to<T, bool>; // 仅为阐释
 
template<class T>
concept boolean-testable = // 仅为阐释
  /*boolean-testable-impl*/<T> && requires(T&& t) {
    {
      !std::forward<T>(t)
    } -> /*boolean-testable-impl*/;
  };

[编辑] 辅助概念 partially-ordered-with

定义于标头 <compare>

template<class T, class U>
concept /*partially-ordered-with*/ = // 仅为阐释
  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;
  };

[编辑] 概念 same_as

template<class T, class U>
concept /*same-as-impl*/ = is_same_v<T, U>; // 仅为阐释
 
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*/ = /* 见描述 */; // 仅用于阐释
 
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*/ = // 仅为阐释
  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*/ = // 仅为阐释
  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*/ = // 仅为阐释
  /*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>;

[编辑] 概念 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)...); // 不要求保持相等性
};

[编辑] 概念 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>;