C++ Concepts: New keywords

By , last updated August 22, 2019

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:

// keyword.cpp
// True concept
void f() requires true {}

int main()
{
    int concept = 1;    // Ill-formed
    int requires = 2;   // Ill-formed
    f();                // Concept call
}

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;
      ^~~~~~~~

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.