How can I combine several return type requirements of C++20 constraints into one return type requirement?
No, you cannot combine type constraints like this, but you can create a named concept
template <class A>
concept allocate_result =
std::regular<A> &&
std::constructible_from<A, std::nullptr_t> &&
std::equality_comparable_with<A, std::nullptr_t> &&
std::random_access_iterator<A>;
and use it
{ a.allocate(0) } -> allocate_result;
{ *a.allocate(0) } -> std::same_as<typename A::value_type&>;
You can also parameterize allocator_result
by the container type and incorporate the last condition:
template <class A, class Cont>
concept allocator_result =
std::regular<A> &&
std::constructible_from<A, std::nullptr_t> &&
std::equality_comparable_with<A, std::nullptr_t> &&
std::random_access_iterator<A> &&
std::same_as<typename std::remove_pointer<A>::type, typename Cont::value_type&>;
template <class A>
concept allocator =
std::copy_constructible<A> &&
std::equality_comparable<A> &&
requires(A a) {
{ *a.allocate(0) } -> allocator_result<A>;
};
For this particular case, you should follow the concept more closely. The return type of allocator_traits<A>::allocate
is required to be allocator_traits<A>::pointer
. So that's what you should test. It is allocator_traits<A>::pointer
that must satisfy the various constraints of contiguous iterator and nullable pointer.
So the code should look like this:
template<typename P>
concept nullable_pointer =
std::regular<P> &&
std::convertible_to<std::nullptr_t, P> &&
std::assignable_from<P&, std::nullptr_t> &&
std::equality_comparable_with<P, std::nullptr_t>
template<typename P>
concept allocator_pointer =
nullable_pointer<P> &&
std::contiguous_iterator<P>;
template<typename A>
concept allocator =
std::copy_constructible<A> &&
std::equality_comparable<A> &&
requires(A a)
{
{ a.allocate(0) } -> allocator_pointer;
};