C++ Concepts: New keywords
Kent - July 14, 2016
The concepts extension introduces 5 new keywords. Only concept
and requires
are implemented, though the GCC documentation states there are 5 new keywords, only 2 are real keywords at the moment.
Trying to use the keywords as variable names or function names are not permitted. Any code which previously used any of the following keywords have to be changed before a concepts-enabled compiler will successfully compile and build.
Concepts Lite keywords
concept
Introduces a concept definition. Concepts are sets of syntactic and semantic requirements on types and their values.
requires
Introduces constraints on template arguments or requirements for a member function of a class template.
Future keywords
assumes
States an expression as an assumption, and if possible, verifies that the assumption is valid. For example, assume(n > 0).
axiom
Introduces an axiom definition. Axioms introduce requirements on values.
forall
Introduces a universally quantified object in an axiom. For example, forall (int n) n + 0 == n)
.
Keyword fail test
Given the following source code listing:
<span class="co">// keyword.cpp</span>
<span class="co">// True concept</span>
<span class="dt">void</span> f() requires <span class="kw">true</span> {}
<span class=“dt”>int</span> main()
{
<span class=“dt”>int</span> concept = <span class=“dv”>1</span>; <span class=“co”>// Ill-formed</span>
<span class=“dt”>int</span> requires = <span class=“dv”>2</span>; <span class=“co”>// Ill-formed</span>
f(); <span class=“co”>// Concept call</span>
}
Build it as usual:
$ g++ -fconcepts keyword.cpp
And watch the compiler complain about the keywords being used as variable names.
keyword.cpp: In function 'int main()':
keyword.cpp:6:14: error: expected unqualified-id before '=' token
int concept = 1;
^
keyword.cpp:7:6: error: expected unqualified-id before 'requires'
int requires = 2;
^~~~~~~~
See Also
- C++ std::sort predicate with templates
- C++ variadic template example
- C++ variadic templates
- Default explicit equality operator
- Mistakes