18 Concepts library [concepts]

18.4 Language-related concepts [concepts.lang]

18.4.1 General [concepts.lang.general]

This subclause contains the definition of concepts corresponding to language features.
These concepts express relationships between types, type classifications, and fundamental type properties.

18.4.2 Concept Same [concept.same]

template<class T, class U> concept same-impl = is_same_v<T, U>; // exposition only template<class T, class U> concept Same = same-impl<T, U> && same-impl<U, T>;
[Note
:
Same<T, U> subsumes Same<U, T> and vice versa.
end note
]

18.4.3 Concept DerivedFrom [concept.derivedfrom]

template<class Derived, class Base> concept DerivedFrom = is_base_of_v<Base, Derived> && is_convertible_v<const volatile Derived*, const volatile Base*>;
[Note
:
DerivedFrom<Derived, Base> is satisfied if and only if Derived is publicly and unambiguously derived from Base, or Derived and Base are the same class type ignoring cv-qualifiers.
end note
]

18.4.4 Concept ConvertibleTo [concept.convertibleto]

The ConvertibleTo concept requires an expression of a particular type and value category to be both implicitly and explicitly convertible to some other type.
The implicit and explicit conversions are required to produce equal results.
template<class From, class To> concept ConvertibleTo = is_convertible_v<From, To> && requires(From (&f)()) { static_cast<To>(f()); };
Let test be the invented function:
To test(From (&f)()) {
  return f();
}
and let f be a function with no arguments and return type From such that f() is equality-preserving.
ConvertibleTo<From, To> is satisfied only if:
  • To is not an object or reference-to-object type, or static_­cast<To>(f()) is equal to test(f).
  • From is not a reference-to-object type, or
    • If From is an rvalue reference to a non const-qualified type, the resulting state of the object referenced by f() after either above expression is valid but unspecified ([lib.types.movedfrom]).
    • Otherwise, the object referred to by f() is not modified by either above expression.

18.4.5 Concept CommonReference [concept.commonref]

For two types T and U, if common_­reference_­t<T, U> is well-formed and denotes a type C such that both ConvertibleTo<T, C> and ConvertibleTo<U, C> are satisfied, then T and U share a common reference type, C.
[Note
:
C could be the same as T, or U, or it could be a different type.
C may be a reference type.
end note
]
template<class T, class U> concept CommonReference = Same<common_reference_t<T, U>, common_reference_t<U, T>> && ConvertibleTo<T, common_reference_t<T, U>> && ConvertibleTo<U, common_reference_t<T, U>>;
Let C be common_­reference_­t<T, U>.
Let t1 and t2 be equality-preserving expressions ([concepts.equality]) such that decltype((t1)) and decltype((t2)) are each T, and let u1 and u2 be equality-preserving expressions such that decltype((u1)) and decltype((u2)) are each U.
T and U model CommonReference<T, U> only if:
  • C(t1) equals C(t2) if and only if t1 equals t2, and
  • C(u1) equals C(u2) if and only if u1 equals u2.
[Note
:
Users can customize the behavior of CommonReference by specializing the basic_­common_­reference class template ([meta.trans.other]).
end note
]

18.4.6 Concept Common [concept.common]

If T and U can both be explicitly converted to some third type, C, then T and U share a common type, C.
[Note
:
C could be the same as T, or U, or it could be a different type.
C might not be unique.
end note
]
template<class T, class U> concept Common = Same<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>()); } && CommonReference< add_lvalue_reference_t<const T>, add_lvalue_reference_t<const U>> && CommonReference< add_lvalue_reference_t<common_type_t<T, U>>, common_reference_t< add_lvalue_reference_t<const T>, add_lvalue_reference_t<const U>>>;
Let C be common_­type_­t<T, U>.
Let t1 and t2 be equality-preserving expressions ([concepts.equality]) such that decltype((t1)) and decltype((t2)) are each T, and let u1 and u2 be equality-preserving expressions such that decltype((u1)) and decltype((u2)) are each U.
T and U model Common<T, U> only if:
  • C(t1) equals C(t2) if and only if t1 equals t2, and
  • C(u1) equals C(u2) if and only if u1 equals u2.
[Note
:
Users can customize the behavior of Common by specializing the common_­type class template ([meta.trans.other]).
end note
]

18.4.7 Integral concepts [concepts.integral]

template<class T> concept Integral = is_integral_v<T>; template<class T> concept SignedIntegral = Integral<T> && is_signed_v<T>; template<class T> concept UnsignedIntegral = Integral<T> && !SignedIntegral<T>;
[Note
:
SignedIntegral<T> can be satisfied even by types that are not signed integral types ([basic.fundamental]); for example, char.
end note
]
[Note
:
UnsignedIntegral<T> can be satisfied even by types that are not unsigned integral types ([basic.fundamental]); for example, bool.
end note
]

18.4.8 Concept Assignable [concept.assignable]

template<class LHS, class RHS> concept Assignable = is_lvalue_reference_v<LHS> && CommonReference<const remove_reference_t<LHS>&, const remove_reference_t<RHS>&> && requires(LHS lhs, RHS&& rhs) { { lhs = std::forward<RHS>(rhs) } -> Same<LHS>; };
Let:
  • lhs be an lvalue that refers to an object lcopy such that decltype((lhs)) is LHS,
  • rhs be an expression such that decltype((rhs)) is RHS, and
  • rcopy be a distinct object that is equal to rhs.
Assignable<LHS, RHS> is satisfied only if
  • addressof(lhs = rhs) == addressof(lcopy).
  • After evaluating lhs = rhs:
    • lhs is equal to rcopy, unless rhs is a non-const xvalue that refers to lcopy.
    • If rhs is a non-const xvalue, the resulting state of the object to which it refers is valid but unspecified ([lib.types.movedfrom]).
    • Otherwise, if rhs is a glvalue, the object to which it refers is not modified.
[Note
:
Assignment need not be a total function ([structure.requirements]); in particular, if assignment to an object x can result in a modification of some other object y, then x = y is likely not in the domain of =.
end note
]

18.4.9 Concept Swappable [concept.swappable]

Let t1 and t2 be equality-preserving expressions that denote distinct equal objects of type T, and let u1 and u2 similarly denote distinct equal objects of type U.
[Note
:
t1 and u1 can denote distinct objects, or the same object.
end note
]
An operation exchanges the values denoted by t1 and u1 if and only if the operation modifies neither t2 nor u2 and:
  • If T and U are the same type, the result of the operation is that t1 equals u2 and u1 equals t2.
  • If T and U are different types that model CommonReference<const T&, const U&>, the result of the operation is that C(t1) equals C(u2) and C(u1) equals C(t2) where C is common_­reference_­t<const T&, const U&>.
The name ranges::swap denotes a customization point object ([customization.point.object]).
The expression ranges::swap(E1, E2) for some subexpressions E1 and E2 is expression-equivalent to an expression S determined as follows:
  • S is (void)swap(E1, E2)222 if E1 or E2 has class or enumeration type ([basic.compound]) and that expression is valid, with overload resolution performed in a context that includes the declarations
    template<class T>
      void swap(T&, T&) = delete;
    template<class T, size_t N>
      void swap(T(&)[N], T(&)[N]) = delete;
    
    and does not include a declaration of ranges::swap.
    If the function selected by overload resolution does not exchange the values denoted by E1 and E2, the program is ill-formed with no diagnostic required.
  • Otherwise, if E1 and E2 are lvalues of array types ([basic.compound]) with equal extent and ranges::swap(*E1, *E2) is a valid expression, S is (void)ranges::swap_­ranges(E1, E2), except that noexcept(S) is equal to noexcept(​ranges::swap(*E1, *E2)).
  • Otherwise, if E1 and E2 are lvalues of the same type T that models MoveConstructible<T> and Assignable<T&, T>, S is an expression that exchanges the denoted values.
    S is a constant expression if
    • T is a literal type ([basic.types]),
    • both E1 = std::move(E2) and E2 = std::move(E1) are constant subexpressions ([defns.const.subexpr]), and
    • the full-expressions of the initializers in the declarations
      T t1(std::move(E1));
      T t2(std::move(E2));
      
      are constant subexpressions.
    noexcept(S) is equal to is_­nothrow_­move_­constructible_­v<T> && is_­nothrow_­move_­assignable_­v<T>.
  • Otherwise, ranges::swap(E1, E2) is ill-formed.
    [Note
    :
    This case can result in substitution failure when ranges::swap(E1, E2) appears in the immediate context of a template instantiation.
    end note
    ]
[Note
:
Whenever ranges::swap(E1, E2) is a valid expression, it exchanges the values denoted by E1 and E2 and has type void.
end note
]
template<class T> concept Swappable = requires(T& a, T& b) { ranges::swap(a, b); };
template<class T, class U> concept SwappableWith = CommonReference<const remove_reference_t<T>&, const remove_reference_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)); };
[Note
:
The semantics of the Swappable and SwappableWith concepts are fully defined by the ranges::swap customization point.
end note
]
[Example
:
User code can ensure that the evaluation of swap calls is performed in an appropriate context under the various conditions as follows:
#include <cassert>
#include <concepts>
#include <utility>

namespace ranges = std::ranges;

template<class T, std::SwappableWith<T> U>
void value_swap(T&& t, U&& u) {
  ranges::swap(std::forward<T>(t), std::forward<U>(u));
}

template<std::Swappable T>
void lv_swap(T& t1, T& t2) {
  ranges::swap(t1, t2);
}

namespace N {
  struct A { int m; };
  struct Proxy { A* a; };
  Proxy proxy(A& a) { return Proxy{ &a }; }

  void swap(A& x, Proxy p) {
    ranges::swap(x.m, p.a->m);
  }
  void swap(Proxy p, A& x) { swap(x, p); }      // satisfy symmetry requirement
}

int main() {
  int i = 1, j = 2;
  lv_swap(i, j);
  assert(i == 2 && j == 1);

  N::A a1 = { 5 }, a2 = { -5 };
  value_swap(a1, proxy(a2));
  assert(a1.m == -5 && a2.m == 5);
}
end example
]
The name swap is used here unqualified.

18.4.10 Concept Destructible [concept.destructible]

The Destructible concept specifies properties of all types, instances of which can be destroyed at the end of their lifetime, or reference types.
template<class T> concept Destructible = is_nothrow_destructible_v<T>;
[Note
:
Unlike the Cpp17Destructible requirements ([tab:destructible]Table *tab:destructible), this concept forbids destructors that are potentially throwing, even if a particular invocation of the destructor does not actually throw.
end note
]

18.4.11 Concept Constructible [concept.constructible]

The Constructible concept constrains the initialization of a variable of a given type with a particular set of argument types.
template<class T, class... Args> concept Constructible = Destructible<T> && is_constructible_v<T, Args...>;

18.4.12 Concept DefaultConstructible [concept.defaultconstructible]

template<class T> concept DefaultConstructible = Constructible<T>;

18.4.13 Concept MoveConstructible [concept.moveconstructible]

template<class T> concept MoveConstructible = Constructible<T, T> && ConvertibleTo<T, T>;
If T is an object type, then let rv be an rvalue of type T and u2 a distinct object of type T equal to rv.
MoveConstructible<T> is satisfied only if
  • After the definition T u = rv;, u is equal to u2.
  • T(rv) is equal to u2.
  • If T is not const, rv's resulting state is valid but unspecified ([lib.types.movedfrom]); otherwise, it is unchanged.

18.4.14 Concept CopyConstructible [concept.copyconstructible]

template<class T> concept CopyConstructible = MoveConstructible<T> && Constructible<T, T&> && ConvertibleTo<T&, T> && Constructible<T, const T&> && ConvertibleTo<const T&, T> && Constructible<T, const T> && ConvertibleTo<const T, T>;
If T is an object type, then let v be an lvalue of type (possibly const) T or an rvalue of type const T.
CopyConstructible<T> is satisfied only if
  • After the definition T u = v;, u is equal to v.
  • T(v) is equal to v.