I assume this was not your intention because the function template algo should accept arguments which model numbers and not just addable. The solution is quite simple. Define and use a concept Number with a meaningful semantic.

First of all, what is a complete set of operations? Here are two complete sets for Arithmetic and Comparable.

Arithmetic: +, -, *, /, +=, -=, *=, /=

Comparable: <, >, <=, >=, ==, !=

Do you want for what the acronym POLA stands for? It stands for Principle Of Least Astonishment. You can quite easily break this principle of good software design if you implement just a partial set of operations.

Here is a very promising example from the guidelines. The concept Minimal in this case supports==, < and +.

An axiom or postulate is a statement that is taken to be true, to serve as a premise or starting point for further reasoning and arguments.

Because C++ does not support axioms, you have to express them with comments. If C++ does support them in the future, you can remove the comment symbol // in front of the axiom in the following example.

The axiom means in this case that the number follow the mathematical rules. In contrast, the concept requires that a Number has to support the binary operations +, -, *, and / and that the result is convertible to T. T is the type of the arguments.

If two concepts have the same requirements, they are logically equivalent. This means the compiler can't distinguish them and, therefore, may not choose automatically the correct one during overload resolution.

To make the rule clear, here is a simplified version of the concept BidirectionalIterator and the refined concept RandomAccessIterator.

std::advance(i, n) increments a given iterator i by n elements. Depending on the value of n, the iterator is incremented or decremented. When the iterator i is a bidirectional iterator, std::advance has to step n times one element forwards or backwards. But when the iterator i is a random access iterator, just n is added to the iterator.

In case of the std::vector<int>, vec.begin() returns an random access iterator and, therefore, the fast variant of std::advance is used.

Each container of the STL creates an iterator specific to its structure. Here is the overview:

What's next?

Three rules to the definition of concepts are left. In particular, the next rule "T.24: Use tag classes or traits to differentiate concepts that differ only in semantics." sound quite interesting. Let's see in the next post what a tag class or traits class is.

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.