In the first example, I specify the concept in the require clause, but I can use the concept Integral just in place of the keyword typename or class. The concept Integral has to be a constant expression that returns a boolean.

I created the concept by using std::is_integral from the type traits library.

Okay, if possible you should use the concepts from the Guidelines Support Library (GSL) or the Ranges TS. Let's see what we have. I ignore the concepts of the GSL because they are mainly part of the Ranges TS. Here are the concepts of the Range TS from the document N4569: Working Draft, C++ Extension for Ranges.

Core language concepts

Same

DerivedFrom

ConvertibleTo

Common

Integral

Signed Integral

Unsigned Integral

Assignable

Swappable

Comparison concepts

Boolean

EqualityComparable

StrictTotallyOrdered

Object concepts

Destructible

Constructible

DefaultConstructible

MoveConstructible

Copy Constructible

Movable

Copyable

Semiregular

Regular

Callable concepts

Callable

RegularCallable

Predicate

Relation

StrictWeakOrder

If you want to know, what each of this concepts means, the already mentioned document N4569 gives you the answers. The concept definitions are based on the type traits library. Here are for example the definitions of the concepts Integral, Signed Integral, and Unsigned Integral.

The functions std::is_integral<T> and std::is_signed<T> are predicates from the type traits library.

Additionally, there are the names used in the text of the C++ standard to define the expectations of the standard library. They are concepts which are not enforced but document the requirement for an algorithm such as std::sort.

template<classRandomIt>void sort( RandomIt first, RandomIt last );

The first overload of std::sort requires two RandomAccessIterator's. Now, I have to say what a RandomAccessIterator is:

A RandomAccessIterator is a BidirectionalIterator that can be moved to point to any element in constant time.

A BidirectionalIterator is a ForwardIteratorthat can be moved in both directions

A ForwardIterator is an Iterator that can read data from the pointed-to element.

The Iterator requirements describe types that can be used to identify and traverse the elements of a container.

For the details to the named requirements used in the text of the C++ standard, read cppreference.com.

auto is an unconstrained concept (placeholder) but you should use constrained concepts. You can use constrained concepts in each situation where you can use unconstrained placeholders (auto). If this is not an intuitive rule?

I defined the concept Integral in line (1). Hence I iterate over integrals in the range-based for-loop in line (2) and the variablesb and integ in line (3) and (4) has to be integrals. I'm not so strict in line (5). Here I'm fine with an unconstrained concept.

The example from the C++ Core Guidelines looks quite innocent but has the potential to revolutionise the way we write templates. Here it is.

template<typename T>// Correct but verbose: "The parameter is// requires Sortable<T> // of type T which is the name of a typevoid sort(T&); // that is Sortable"template<Sortable T>// Better (assuming support for concepts): "The parameter is of type Tvoid sort(T&); // which is Sortable"voidsort(Sortable&); // Best (assuming support for concepts): "The parameter is Sortable"

This example shows three variations to declare the function template sort. All variations are semantically equivalent and require that the template parameter supports the concept Sortable. The last variation looks like a function declaration but is a function template declaration because the parameter is a concept and not a concrete type. To say it once more:sort becomes due to the concept parameter a function template.

What's next?

The C++ core guidelines say: "Defining good concepts is non-trivial. Concepts are meant to represent fundamental concepts in an application domain." Let's see what that mean in my next post.

Get your e-book at Leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11,C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.

Get your interactive course at educative

Modern C++ Concurrency in Practice: Get the most out of any machine

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.