In the book by Bjarne Straustrup "Programming. Principles and practice using C ++, 2nd edition.", Chapter 19, paragraph 19.3.3 it is written that in C ++ 14 we can use and apply to the template arguments some concept or requirements of the template.

template<typename T> // Для всех типов T requires Element<T>() // таких, что является Element class vector { // . . . }; 

Here are a couple of concepts that are provided in the book (there are many of them):

 Element<E> (): Е может быть элементом контейнера Number<N> () : N ведет себя как число, поддерживая операции +, -, * и /. 

But this code is not compiled. Please explain if there is a keyword requires and, in general, the concept of templates in C ++?

  • Not yet. In the future will be. Now everything is implemented through is_detected and SFINAE - int3
  • I do not have this book, but I assume that the author hopes that we can in C ++ 14, but does not approve of this. - ixSci
  • If not mistaken, it was excluded from the standard. - Qwertiy

1 answer 1

There are no concepts in C ++ 14, and they will not be in C ++ 17.

Currently, concepts are implemented only in GCC 6.1+, and are included with the -fconcepts command-line -fconcepts :

 > type main.cpp #include <iostream> template<typename T> requires sizeof(T) < 4 void f() { std::cout << "< 4\n"; } template<typename T> requires sizeof(T) >= 4 void f() { std::cout << ">= 4\n"; } int main() { f<short>(); f<double>(); } > g++ -fconcepts -std=c++14 -Wall -pedantic main.cpp && a < 4 >= 4 

A brief syntax description can be found at cppreference .

  • And is it exactly the "concept" in Russian, or is it still a "concept"? - αλεχολυτ
  • 3
    It seems to me that now both translations are equivalent. The English term is concept, while the words concept (an abstract or generic idea) and conception (the sum of a person's ideas) mean about the same thing. In Russian, "concept" is a concept paper used in words, concept car, concept art. A “concept” is a more “Russian” word that is more like the original English term. - Abyx