What are restricted templates?

Herb Sutters mentioned limited patterns (aka Concepts Lite) in the conversation: Modern C ++: what you need to know .

I know that boost has had a conceptual package for centuries, which allows you to print error messages when the template subtraction mechanism does not find operators, functions or access violation patterns.

I came across references to the isocpp blog that there is already an experimental gcc document branch offering Concepts Lite . However, looking through the current C ++ 14 project, I could not find any hints whether this would already be part of C++14 .

So the questions are simple:

  • Will Concepts Lite be part of C ++ 14? (A reference in the standard is preferable. I could not find it, and I am not very familiar with the standard.)
  • What is the correct syntax? (Herb's proposal and slides diverge here, and I don't know which one is more relevant)
  • Could you give a minimal example of a constraint (predicate) and a constrained template?

NOTE: if you wait long enough, I will try to run the gcc branch and at least say something about the experimental implementation, but that does not mean the syntax is correct.

+6
source share
1 answer

Lite Concepts is a “limitation” of part of the complete concept design for C ++. This is described in detail in the N3701 "Concepts Lite" . Chapter 2 is a short tutorial that quickly goes through the basic principles and their applications, which is great for people who don't want to read all 56 pages.

Lite concepts will not be part of C ++ 14, they will be released as a separate Technical Specification at the end of this year. The latest draft for the wording of TS is N3929 Lite Literature .

There are several different syntax options for constraints. Example code that Herb used in a conversation:

 auto mean(const Sequence& seq) { auto n = 0.0; for (auto x : seq) n += x; return n / seq.size(); } 

referred to as “short syntax” because it is the shorter equivalent of detailed syntax:

 template <typename __T> requires Sequence<__T>() auto mean(const __T& seq) { auto n = 0.0; for (auto x : seq) n += x; return n / seq.size(); } 

Both of them indicate that the mean function template can be created by any type that models the concept of Sequence . For simplicity, suppose that the requirements for Sequence are just what we need to implement mean : (a) the begin and end members that return iterators, and (b) the size member function that returns some kind of integral type. We could define the concept as:

 template <typename T> concept bool Sequence() { return requires(T t) { {t.size()} -> Integral; {t.begin()} -> InputIterator; {t.end()} -> InputIterator; requires Same<decltype(t.begin()), decltype(t.end())>(); } } 

assuming straightforward definitions of Integral , InputIterator and Same . This definition of the concept ensures that for some invented value t test type t :

  • t.size() is a valid expression and returns a type that models the concept of Integral .
  • t.begin() valid and returns a type that models the InputIterator .
  • The same goes for t.end() .
  • InputIterator returned by t.begin() is of the same type as t.end() returned.
+3
source

All Articles