c++-concepts

Concept definition requiring a constrained template member function

醉酒当歌 提交于 2020-04-10 08:23:30
问题 Note: everything that follows uses the Concepts TS implementation in GCC 6.1 Let's say I have a concept Surface , like the following: template <typename T> concept bool Surface() { return requires(T& t, point2f p, float radius) { { t.move_to(p) }; { t.line_to(p) }; { t.arc(p, radius) }; // etc... }; } Now I want to define another concept, Drawable , which matches any type with a member function: template <typename S> requires Surface<S>() void draw(S& surface) const; i.e. struct triangle {

Concept definition requiring a constrained template member function

半城伤御伤魂 提交于 2020-04-10 08:21:12
问题 Note: everything that follows uses the Concepts TS implementation in GCC 6.1 Let's say I have a concept Surface , like the following: template <typename T> concept bool Surface() { return requires(T& t, point2f p, float radius) { { t.move_to(p) }; { t.line_to(p) }; { t.arc(p, radius) }; // etc... }; } Now I want to define another concept, Drawable , which matches any type with a member function: template <typename S> requires Surface<S>() void draw(S& surface) const; i.e. struct triangle {

Something like `declval` for concepts

只谈情不闲聊 提交于 2020-04-06 08:08:28
问题 When you are working with templates and with decltype you often need an instance of a certain type even though you do not have any the moment. In this case, std::declval<T>() is incredibly useful. This creates an imaginary instance of the type T . Is there a something similar for concepts? i.e. a function which would create and imaginary type for a concept. Let me give you an example(a bit contrived but should serve the purpose): Let's define a concept Incrementable template <typename T>

Something like `declval` for concepts

若如初见. 提交于 2020-04-06 08:08:12
问题 When you are working with templates and with decltype you often need an instance of a certain type even though you do not have any the moment. In this case, std::declval<T>() is incredibly useful. This creates an imaginary instance of the type T . Is there a something similar for concepts? i.e. a function which would create and imaginary type for a concept. Let me give you an example(a bit contrived but should serve the purpose): Let's define a concept Incrementable template <typename T>

Wildcard for C++ concepts saying “accepting anything for this template argument”

痞子三分冷 提交于 2020-03-20 07:39:38
问题 Is there a way to allow a concept with template arguments , to be ok with any template parameter provided? I.e. some kind of wildcard magic for template argument placeholder? A usage example: template<class Me, TestAgainst> concept derived_from_or_same_as = std::same_as<Me, TestAgainst> || std::derived_from<decltype(p.first), First>; Above is needed because unfortunately primitive types behave differently than class types for is_base_of and derived_from . Now we can define a Pair concept that

Wildcard for C++ concepts saying “accepting anything for this template argument”

拟墨画扇 提交于 2020-03-20 07:37:55
问题 Is there a way to allow a concept with template arguments , to be ok with any template parameter provided? I.e. some kind of wildcard magic for template argument placeholder? A usage example: template<class Me, TestAgainst> concept derived_from_or_same_as = std::same_as<Me, TestAgainst> || std::derived_from<decltype(p.first), First>; Above is needed because unfortunately primitive types behave differently than class types for is_base_of and derived_from . Now we can define a Pair concept that

Wildcard for C++ concepts saying “accepting anything for this template argument”

别说谁变了你拦得住时间么 提交于 2020-03-20 07:37:37
问题 Is there a way to allow a concept with template arguments , to be ok with any template parameter provided? I.e. some kind of wildcard magic for template argument placeholder? A usage example: template<class Me, TestAgainst> concept derived_from_or_same_as = std::same_as<Me, TestAgainst> || std::derived_from<decltype(p.first), First>; Above is needed because unfortunately primitive types behave differently than class types for is_base_of and derived_from . Now we can define a Pair concept that

if constexpr and requires-expression for ad-hoc concepts checking

北城余情 提交于 2020-03-18 04:30:10
问题 Let's say, given C++17's if constexpr and Concepts TS (for instance, in recent gcc versions), we'd like to check if a type in a template function has a nested type: #include <iostream> struct Foo { using Bar = int; }; template<typename T> void doSmth(T) { if constexpr (requires { typename T::Bar; }) std::cout << "has nested! " << typename T::Bar {} << std::endl; else std::cout << "no nested!" << std::endl; } int main() { doSmth(Foo {}); //doSmth(0); } The documentation for concepts is scarce,

Templated requires-expression

让人想犯罪 __ 提交于 2020-03-16 07:36:06
问题 I want a Functor concept in C++20. A functor is a higher-kinded type that can be mapped over. A simple example is std::optional ; with a function from type A to type B and a std::optional<A> , you can easily create a std::optional<B> by applying the function to the value if it exists and returning an empty optional otherwise. This operation is called fmap in Haskell. template<typename A, typename B> std::optional<B> fmap(std::function<B(A)> f, std::optional<A> fa) { if (!fa) { return std:

C++0x will no longer have concepts. Opinions? How will this affect you?

守給你的承諾、 提交于 2020-01-29 12:24:09
问题 At the July 2009 C++0x meeting in Frankfurt, it was decided to remove concepts from C++0x. Personally, I am disappointed but I'd rather have an implementable C++0x than no C++0x. They said they will be added at a later date. What are your opinions on this decision/issue? How will it affect you? 回答1: Personally I'm not too unhappy of the removal as the purpose of concepts were to mainly improve compile time error messages, as Jeremy Siek, one of the co-authors of the Concepts proposal, writes