During the C++11 standards development cycle, much work was done on a feature called "concepts" which aimed at providing systematic constraints on templates. Concepts was deferred from C++11 for lack of time to complete it, but work has continued.
In January 2012, the results of a major "concepts summit" were published as a 133-page report titled "A Concept Design for the STL" (WG21 paper N3351).
Now, a draft of new paper is available proposing a very useful subset of concepts, dubbed "Concepts Lite", for near-term consideration including at the spring ISO C++ meeting in Bristol, UK, this April. For example, imagine writing this template:
template<Sortable Cont> void sort(Cont& container);
and when you call it like this:
list<int> lst = ...; // oops, bidirectional iterators sort(lst); // today, results in very long "template spew" error message
getting this short and non-cryptic error message:
error: no matching function for call to ‘sort(list<int>&)’ sort(l); ^ note: candidate is: note: template<Sortable T> void sort(T) void sort(T t) { } ^ note: template constraints not satisfied because note: 'T' is not a/an 'Sortable' type [with T = list<int>] since note: 'declval<T>()[n]' is not valid syntax
That's an actual error message from the prototype GCC implementation linked below.
We're very excited about this feature and its continued progress. Here are links to the draft of the new paper:
Concepts Lite: Constraining Templates with Predicates (PDF) (Google Docs)
From the Introduction:
In this paper, we introduce template constraints (a.k.a., “concepts lite”), an extension of C++ that allows the use of predicates to constrain template arguments. The proposed feature is minimal, principled, and uncomplicated. Template constraints are applied to enforce the correctness of template use, not the correctness of template definitions. The design of these features is intended to support easy and incremental adoption by users. More precisely, constraints:
- allow programmers to directly state the requirements of a set of template arguments as part of a template’s interface,
- support function overloading and class template specialization based on constraints,
- fundamentally improve diagnostics by checking template arguments in terms of stated intent at the point of use, and
- do all of this without any runtime overhead or longer compilation times.
This work is implemented as a branch of GCC-4.8 and is available for download at http://concepts.axiomatics.org/~ans/. The implementation includes a compiler and a modified standard library that includes constraints. Note that, as of the time of writing, all major features described in this report have been implemented.
Related links:
- Bjarne Stroustrup, Andrew Sutton, et al., "A Concept Design for the STL" (ISO/IEC JTC1/SC22/WG21 N3351, January 2012).
Add a Comment
Comments are closed.