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 &#39;int main()&#39;:
keyword.cpp:6:14: error: expected unqualified-id before &#39;=&#39; token
  int concept = 1;
              ^
keyword.cpp:7:6: error: expected unqualified-id before &#39;requires&#39;
  int requires = 2;
      ^~~~~~~~

See Also

Comments

Any comments? Create a new discussion on GitHub.
There used to be an inline comment form here, but it was removed.