c++-concepts

Is it possible to define a callable concept that includes functions and lambdas?

▼魔方 西西 提交于 2020-06-16 19:20:13
问题 I want to define a concept that would accept all callable objects. Here's what I have done so far: template<typename F> concept Func = std::is_function_v<std::remove_pointer_t<std::decay_t<F>>> || (requires (F f) { std::is_function_v<decltype(f.operator())>; }); bool is_callable(Func auto&&) { return true; } bool is_callable(auto&&) { return false; } Yet if I define those: auto f = [](auto a, auto b, auto c, auto d, auto e) { return a * b * c * d * e; }; int g(int a, int b) { return a + b; }

Why is std::back_inserter_iterator not WeaklyIncrementable in RangeV3?

荒凉一梦 提交于 2020-06-11 07:34:12
问题 To my surprise this Concept-like assertion fails in RangeV3. #include<vector> #include<range/v3/algorithm/copy.hpp> int main(){ static_assert(ranges::WeaklyIncrementable<std::back_insert_iterator<std::vector<double> >>()); } Why is that? This, among other things means that I cannot use the ranges::copy algorithm as I use to do with std::copy . std::vector<double> w(100); std::vector<double> v; ranges::copy( begin(w), end(w), std:back_inserter(v) ); // compilation error, concept not fulfilled.

C++ Concept that requires a member function with an OutputIterator as parameter

时光总嘲笑我的痴心妄想 提交于 2020-06-11 06:52:06
问题 I am playing with concepts and hit a roadblock. Or maybe it is just my mind that is blocked. I want to create a class that buffers a "bulk-readable" data source. Such a datasource should have a member function that accepts an OutputIterator and has a signature like: template<typename It> size_t read(It firstItem, size_t max) My idea was to define a BulkReadable concept similar to: template<typename Source> concept bool BulkReadable = requires(Source s, Iter out, size_t max) { {s.read(out, max

C++ Concepts with multiple template arguments

自作多情 提交于 2020-05-24 21:12:27
问题 Bjarne Stroustrup recently published a report on C++ Concepts where he mentions something that seemed surprising to me. The example (in Section 7.1) uses "shorthand template notation" and essentially goes like this: void foo1(auto x,auto y); // x and y may have different types (1) void foo2(SomeConcept x,SomeConcept y); // x and y must have the same type (2) To me personally, this seems very counter-intuitive; in fact, I would expect foo2 to accept values x,y of different types as long as the

Dependencies when using C++20 conceptual template function specialization

独自空忆成欢 提交于 2020-05-14 05:53:27
问题 I was doing some tests with the following C++20 code (built with current GCC10): template <typename ToT, typename FromT> ToT myfunction(const FromT& pFrom) = delete; template <typename ToT, typename FromT> struct myclass { inline ToT myclassmethod(const FromT& pFrom) { return myfunction<ToT, FromT>(pFrom); } }; /* C++20 concepts code */ template <std::same_as<std::string> ToT, std::integral FromT> inline ToT myfunction(const FromT& pFrom) { return std::to_string(pFrom); } template <std::same

Dependencies when using C++20 conceptual template function specialization

十年热恋 提交于 2020-05-14 05:52:07
问题 I was doing some tests with the following C++20 code (built with current GCC10): template <typename ToT, typename FromT> ToT myfunction(const FromT& pFrom) = delete; template <typename ToT, typename FromT> struct myclass { inline ToT myclassmethod(const FromT& pFrom) { return myfunction<ToT, FromT>(pFrom); } }; /* C++20 concepts code */ template <std::same_as<std::string> ToT, std::integral FromT> inline ToT myfunction(const FromT& pFrom) { return std::to_string(pFrom); } template <std::same

How is the best constrained function template selected with concepts?

≡放荡痞女 提交于 2020-05-13 09:45:40
问题 In a presentation of concepts something like this was shown: template <bidirectional_iterator It> void sort(It begin, It end); // #1 template <random_access_iterator It> void sort(It begin, It end); // #2 std::list<int> l{}; sort(l.begin(), l.end()); // #A -> calls #1 std::vector<int> v{}; sort(v.begin(), v.end()); // #B -> calls #2 For the call #A it's simple: only sort #1 is viable as the constraint random_access_iterator is not satisfied so it calls #1 . But for the call #B both sort s are

How is the best constrained function template selected with concepts?

蓝咒 提交于 2020-05-13 09:42:07
问题 In a presentation of concepts something like this was shown: template <bidirectional_iterator It> void sort(It begin, It end); // #1 template <random_access_iterator It> void sort(It begin, It end); // #2 std::list<int> l{}; sort(l.begin(), l.end()); // #A -> calls #1 std::vector<int> v{}; sort(v.begin(), v.end()); // #B -> calls #2 For the call #A it's simple: only sort #1 is viable as the constraint random_access_iterator is not satisfied so it calls #1 . But for the call #B both sort s are

Why can't concept refinement use the terse syntax

瘦欲@ 提交于 2020-04-29 10:38:19
问题 When refining concepts, the way it is consistently done in the standard is to fully write out the concept being refined. For instance, in [concepts.integral], SignedIntegral refines Integral like so: template<class T> concept Integral = is_integral_v<T>; template<class T> concept SignedIntegral = Integral<T> && is_signed_v<T>; Why can't the refined concept be written as: template<Integral T> concept SignedIntegral2 = is_signed_v<T>; SignedIntegral2 seems to have the same obvious meaning of

What is the correct syntax for concept in combination with a templated struct or class?

主宰稳场 提交于 2020-04-16 06:08:09
问题 Recently I was using concept s to define different constructors for a template d struct . Here is the code: #include <iostream> namespace detail { template<typename T, typename U > concept SameHelper = std::is_same_v<T, U>; } template<typename T, typename U > concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>; template<typename T> concept trivial = same_as<T, bool> || same_as<T, char>; template<typename T> concept not_trivial = !trivial<T>; template<typename T> struct Foo