c++-concepts

C++ concepts placeholder type deduction

依然范特西╮ 提交于 2019-12-03 12:08:40
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> requires D<T> void fn() {} int main() { // Fails with: unable to deduce placeholder type 'C<char>'

Hypothetical, formerly-C++0x concepts questions

拥有回忆 提交于 2019-12-03 05:56:17
( 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 controversy that has followed) After having read some introductory material on concepts as C++0x (until

Why use std::forward in concepts?

老子叫甜甜 提交于 2019-12-03 04:39:34
I was reading the cppreference page on Constraints and noticed this example: // example constraint from the standard library (ranges TS) template <class T, class U = T> concept bool Swappable = requires(T t, U u) { swap(std::forward<T>(t), std::forward<U>(u)); swap(std::forward<U>(u), std::forward<T>(t)); }; I'm puzzled why they're using std::forward . Some attempt to support reference types in the template parameters? Don't we want to call swap with lvalues, and wouldn't the forward expressions be rvalues when T and U are scalar (non-reference) types? For example, I would expect this program

How are c++ concepts different to Haskell typeclasses?

帅比萌擦擦* 提交于 2019-12-03 04:12:15
问题 Concepts for C++ from the Concepts TS have been recently merged into GCC trunk. Concepts allow one to constrain generic code by requiring types to satisfy the conditions of the concept ('Comparable' for instance). Haskell has type classes. I'm not so familiar with Haskell. How are concepts and type classes related? 回答1: Concepts (as defined by the Concepts TS) and type classes are related only in the sense that they restrict the sets of types that can be used with a generic function. Beyond

Why do we require requires requires?

我的梦境 提交于 2019-12-03 01:15:41
问题 One of the corners of C++20 concepts is that there are certain situations in which you have to write requires requires . For instance, this example from [expr.prim.req]/3: A requires-expression can also be used in a requires-clause ([temp]) as a way of writing ad hoc constraints on template arguments such as the one below: template<typename T> requires requires (T x) { x + x; } T add(T a, T b) { return a + b; } The first requires introduces the requires-clause , and the second introduces the

How are c++ concepts different to Haskell typeclasses?

 ̄綄美尐妖づ 提交于 2019-12-02 17:29:20
Concepts for C++ from the Concepts TS have been recently merged into GCC trunk. Concepts allow one to constrain generic code by requiring types to satisfy the conditions of the concept ('Comparable' for instance). Haskell has type classes. I'm not so familiar with Haskell. How are concepts and type classes related? Concepts (as defined by the Concepts TS) and type classes are related only in the sense that they restrict the sets of types that can be used with a generic function. Beyond that, I can only think of ways in which the two features differ. I should note that I am not a Haskell expert

Why do we require requires requires?

风流意气都作罢 提交于 2019-12-02 16:32:25
One of the corners of C++20 concepts is that there are certain situations in which you have to write requires requires . For instance, this example from [expr.prim.req]/3 : A requires-expression can also be used in a requires-clause ([temp]) as a way of writing ad hoc constraints on template arguments such as the one below: template<typename T> requires requires (T x) { x + x; } T add(T a, T b) { return a + b; } The first requires introduces the requires-clause , and the second introduces the requires-expression . What is the technical reason behind needing that second requires keyword? Why

C++ Concepts Same and Assignable

血红的双手。 提交于 2019-12-02 01:28:15
I have been experimenting with C++ concepts recently. I am trying the definitions from the following Ranges Extensions document: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4569.pdf The definitions and usages of Same are confusing me. For reasons unknown to me, the authors did not give an explicit definition. So I am using: template <class T, class U> concept bool Same() { return std::is_same<T, U>::value; } The problem is that the document gives the following definition for Assignable : template <class T, class U> concept bool Assignable() { return Common<T, U>() && requires(T&&

In which access control context are evaluated concepts?

元气小坏坏 提交于 2019-12-01 18:06:48
This question is a follow up to this one [temp.concept]/5 says: A concept is not instantiated ([temp.spec]). [ Note: An id-expression that denotes a concept specialization is evaluated as an expression ([expr.prim.id]). [...]] So maybe an expression that name a concept specialization can have different value because of accessibility. If it were the case, I wonder in which context would be evaluated the expression: The context of the concept definition; The context of the expression; The context of the expression recursively applied to concepts expression appearing in concepts definition? For

In which access control context are evaluated concepts?

♀尐吖头ヾ 提交于 2019-12-01 16:17:04
问题 This question is a follow up to this one [temp.concept]/5 says: A concept is not instantiated ([temp.spec]). [ Note: An id-expression that denotes a concept specialization is evaluated as an expression ([expr.prim.id]). [...]] So maybe an expression that name a concept specialization can have different value because of accessibility. If it were the case, I wonder in which context would be evaluated the expression: The context of the concept definition; The context of the expression; The