std::swappable, std::swappable_with

来自cppreference.com
<tbody> </tbody>
在标头 <concepts> 定义
template< class T > concept swappable = requires(T& a, T& b) { ranges::swap(a, b); };
(1) (C++20 起)
template< class T, class U > concept swappable_with = std::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)); };
(2) (C++20 起)

概念 swappable<T> 指定 T 类型的左值可交换。

概念 swappable_with<T, U> 指定属于 TU 所编码的类型和值类别的表达式彼此可交换。 swappable_with<T, U> 仅当调用 ranges::swap(t, u) 交换 tu 的值,即给定等于 t 的独立对象 t2 和等于 u 的独立对象 u2 ,求值 ranges::swap(t, u)ranges::swap(u, t)t2 等于 uu2 等于 t 时,才得到满足。

相等性保持

标准库概念的 requires 表达式中声明的表达式都要求保持相等性(除非另外说明)。

引用

  • C++23 标准(ISO/IEC 14882:2024):
  • 18.4.9 Concept swappable [concept.swappable]
  • C++20 标准(ISO/IEC 14882:2020):
  • 18.4.9 Concept swappable [concept.swappable]