c++-concepts

How can I combine several return type requirements of C++20 constraints into one return type requirement?

荒凉一梦 提交于 2020-08-24 05:48:25
问题 Currently, I have implemented the Allocator concept (which refers to the Boost proposal) using C++20 constraints and concepts: #include <concepts> #include <iterator> template <class A> concept allocator = std::copy_constructible<A> && std::equality_comparable<A> && requires(A a) { { a.allocate(0) } -> std::regular; { a.allocate(0) } -> std::constructible_from<std::nullptr_t>; { a.allocate(0) } -> std::equality_comparable_with<std::nullptr_t>; { a.allocate(0) } -> std::random_access_iterator;

How can I combine several return type requirements of C++20 constraints into one return type requirement?

穿精又带淫゛_ 提交于 2020-08-24 05:46:06
问题 Currently, I have implemented the Allocator concept (which refers to the Boost proposal) using C++20 constraints and concepts: #include <concepts> #include <iterator> template <class A> concept allocator = std::copy_constructible<A> && std::equality_comparable<A> && requires(A a) { { a.allocate(0) } -> std::regular; { a.allocate(0) } -> std::constructible_from<std::nullptr_t>; { a.allocate(0) } -> std::equality_comparable_with<std::nullptr_t>; { a.allocate(0) } -> std::random_access_iterator;

How can unspecified types be used in C++20 'requires' expressions?

ぐ巨炮叔叔 提交于 2020-08-20 11:29:32
问题 I'm trying to write a C++20 concept to express the requirement that a type have a certain method, which takes an argument, but for the purposes of this concept I don't care what the argument type is. I've tried to write something like: template <typename T> concept HasFooMethod = requires(T t, auto x) { { t.Foo(x) } -> std::same_as<void>; }; however, both gcc and clang reject this, giving an error that 'auto' cannot be used in the parameter list of a requires expression this way. An

Why C++20 doesn't use `requires` to restrict the T for atomic<T>?

时光怂恿深爱的人放手 提交于 2020-08-07 18:27:41
问题 A generic std::atomic<T> is required to have a T that is Copy Constructible and Copy Assignable : [atomics.types.generic]/1 The program is ill-formed if any of (1.1) is_­trivially_­copyable_­v<T> , (1.2) is_­copy_­constructible_­v<T> , (1.3) is_­move_­constructible_­v<T> , (1.4) is_­copy_­assignable_­v<T> , or (1.5) is_­move_­assignable_­v<T> is false . Above is not new to C++20. Compilers may use static_assert to issue an error for a non-conforming T. However, C++20 could use formal

Will there be a concept for arithmetic types in C++ standard library?

北城以北 提交于 2020-08-06 08:09:46
问题 I've been looking through concepts library on C++ reference and i couldn't find a concept for arithmetic types. I couldn't also find it in p0898. I think such concept would be very helpful. Insted of doing: template <typename T> T some_function(T arg) requires std::integral<T> || std::floating_point<T> { /* functions body */ } I could just do: template <std::arithmetic T> T some_function(T arg) { /* functions body */ } I could obviously define it myself and it wouldn't be hard (ex. template

Will there be a concept for arithmetic types in C++ standard library?

余生长醉 提交于 2020-08-06 08:09:45
问题 I've been looking through concepts library on C++ reference and i couldn't find a concept for arithmetic types. I couldn't also find it in p0898. I think such concept would be very helpful. Insted of doing: template <typename T> T some_function(T arg) requires std::integral<T> || std::floating_point<T> { /* functions body */ } I could just do: template <std::arithmetic T> T some_function(T arg) { /* functions body */ } I could obviously define it myself and it wouldn't be hard (ex. template

Can concepts be used with template template parameters?

你。 提交于 2020-06-25 05:26:09
问题 Let us consider following code: #include <concepts> template<typename X> struct Concrete_M { X f() const { return X{}; } }; struct Concrete_X {}; template<typename T, typename X> concept M = requires (T t) { { t.f() } -> std::convertible_to<X>; }; template<M<Concrete_X>> struct C {}; const C<Concrete_M<Concrete_X>> c{}; Can I use following template template parameter T ? template<template<typename> typename T, typename X> concept M = requires (T<X> t) { { t.f() } -> std::convertible_to<X>; };