c++-concepts

Can the types of parameters in template functions be inferred?

时光毁灭记忆、已成空白 提交于 2020-01-24 10:47:04
问题 I'm writing some template functions in C++, but I'm not sure if it's possible to define a template function that infers the types of its parameters. I tried to define a template with inferred parameter types, but this example won't compile: template <auto> auto print_stuff(auto x, auto y) { std::cout << x << std::endl; std::cout << y << std::endl; } It works when I give a unique name to each parameter type, but this seems somewhat redundant: #include <iostream> #include <string> template

C++ Concepts - Can I have a constraint requiring a function be present in a class?

北战南征 提交于 2020-01-14 10:42:54
问题 I have a simple code snippet below, which compiles using: g++-9 -std=c++2a -fconcepts This is trying to define a concept that requires the presence of a function. I would expect the output to be "yes" but it's not... Any idea why? Thanks. #include <iostream> template <typename T> concept bool HasFunc1 = requires(T) { { T::func1() } -> int; }; struct Test { int func1() { return 5; } }; int main() { if constexpr (HasFunc1<Test>) std::cout << "yes\n"; } 回答1: You are testing for presence of a

What is C++ Technical Specification?

无人久伴 提交于 2020-01-13 08:03:12
问题 Concepts-lite C++ (proposal N3701) feature is not included in C++1y standard, but it is said it will be published as Technical Specification. What does it exactly mean? Will it automatically become a standard feature in next C++ releases? 回答1: I usually don't like copy-paste answer's, but I think it is pretty well explained here: Starting in 2012, the committee has transitioned to a “decoupled” model where major pieces of work progress independently from the Standard itself and can be

Why function parameters can not be static

大憨熊 提交于 2019-12-25 02:15:40
问题 Can anyone please tell me that why the functions parameters can not be static ? Is this the reason that function parameters are declared on Stack and gets de-allocated when function return? There is no way to retain parameter values? Just confused. Please clarify. Thanks. 回答1: The keyword static can probably be seen as somewhat "overloaded". The following usage-options are all viable: Static local variables Static global variables Static member variables Static global functions Static member

Why have both variable and function concepts in C++ Concepts TS?

淺唱寂寞╮ 提交于 2019-12-23 17:25:32
问题 I've been looking at the C++1z N4377 Concepts TS draft that is being implemented in GCC 6, and I don't understand the purpose of having two different kinds of concepts: variable concepts and function concepts. The relevant part of the draft for function concepts is [dcl.spec.concept (5.4)] The declaration shall have a function-body equivalent to { return E; } where E is a constraint-expression (14.10.1.3). and for variable concepts, in the next paragraph [(6.3)] : The initializer shall be a

C++ Concepts: Can I define a concept that is itself a template?

岁酱吖の 提交于 2019-12-23 12:23:18
问题 Sorry if the question isn't too clear. I'm not sure the best way to phrase it (feel free to edit!). I think an example would be the most clear: I am attempting to define a Monad concept based off of the Haskell definition. The bind operator ( >>= ) requires that a Monad of type A can be bound to a function that takes an A and returns a Monad of type B . I can define A in terms of a value_type typedef but how do I define type B in my concept? template <typename M> concept bool Monad() { return

How can we express Concept in UML diagram?

五迷三道 提交于 2019-12-23 01:35:08
问题 Does UML Class Diagram support expressing Concept? Also, is there any other diagrams that expresses Concept? Just in case of misunderstanding, I mean the "Concept" in C++ and generic programming. 回答1: After reading shortly Wikipedia explanation of C++ "concepts" it looks to me like tool with same goals as generic classes and type constraints in C# If I understood it correctly then by following older Stack Overflow question Representing a C# Generic Method in a UML Class Diagram it turns out

How to check if an iterator is an output_iterator in c++?

江枫思渺然 提交于 2019-12-22 09:16:12
问题 template<typename Iterator> void put_value(Iterator pos, int n) { static_assert(IsOutputIterator<Iterator>); // // How to implement IsOutputIterator? // *pos = n; } std::iterator_traits<Iterator>::iterator_category doesn't help. For example: vector<int>::iterator is obvious an output_iterator , but std::iterator_traits<vector<int>::iterator>::iterator_category will returns random_access_iterator , which might not be an output_iterator , say a const_iterator . Is there any viable way to check

C++ concepts placeholder type deduction

﹥>﹥吖頭↗ 提交于 2019-12-21 03:59:52
问题 In the Ranges spec N4622 the Same concept is defined to take two types T and U , but is sometimes used inside requires with just one like: { t } -> Same<T>; What's the rule that enables the deduction for the 2nd type U ? (e.g. from the Concepts spec N4630) The simplest similar example would be: template <class T, class U> concept bool C = (sizeof(T) == sizeof(U)) && (sizeof(U) != 1); template <class T> concept bool D = requires(T t){ // How is U deduced here? {t} -> C<T>; }; template <class T

Hypothetical, formerly-C++0x concepts questions

对着背影说爱祢 提交于 2019-12-20 19:57:20
问题 ( Preamble: I am a late follower to the C++0x game and the recent controversy regarding the removal of concepts from the C++0x standard has motivated me to learn more about them. While I understand that all of my questions are completely hypothetical -- insofar as concepts won't be valid C++ code for some time to come, if at all -- I am still interested in learning more about concepts, especially given how it would help me understand more fully the merits behind the recent decision and the