This post is about template interfaces which are due to the C++ core guidelines: "...a critical concept", because a template interface is "a contract between a user and an implementer - and should be carefully designed.".

Now, one question remains: What is the issue if you specify non-essential properties. This means that your concepts are strongly bound the implementation. The result may be that a small change of the implementation changes your concepts. In the end, you interface becomes quite unstable.

Since C++11 we have templates aliases. A template alias is a name that refers to a family of types. Using them makes your code more readable and helps you to get rid of type traits. My previous post C++ Core Guidelines: Definition of Concepts, the Second provides more information to type traits.

Let's see the guidelines mean by readability. The first example uses type traits:

There are two arguments from the readability perspective to prefer using over typedef. First, using comes first when used. Second, using feels quite similar to auto. Additionally, using can easily be used for template aliases.

The first two lines define a pointer to a function (PFI and PFI2) which takes an int and returns an int. In the first case typedef is used and in the second line using. The last two lines define a function template (PFT2) which takes a type parameter T and returns an int. The line (1) is not valid.

The primary reason that we have too many make_ functions such as std::make_tuple or std::make_unique is that a function template can deduce its template arguments from it function arguments. During this process, the compiler applies a few simple conversions such as removing the outermost const/volatile qualifier and decaying C-arrays and functions to a pointer to the first element of the C-arrray or a pointer to the function.

This automatic template argument deduction makes our life as a programmer much easier.

Sad to say but automatic template type deduction is in C++ only available for function templates. Why? Constructors of class templates are special static function. Right! With C++17, the compiler can deduce its template arguments from its constructor arguments. Here is the way to define myTuple in C++17.

std::tuple myTuple = {2017, 20.17, "C++17"};

An obvious effect of this C++17 feature is that most of the make_ function become obsolete with C++17.

Teachability of C++

I have to admit, I like this C++17 feature. As a C++ trainer, my job is it to explain this difficult stuff. The more symmetric C++ becomes the easier is it for me to speak about the general ideas. Now I can say: "A template can automatically deduce its template arguments from its function arguments.". In the past, I had to say this works only for function templates.

The usage of the function template showMe or the class template ShowMe feels the same. From the user perspective, you don't know that you use a template.

With a current GCC 8.2, the program compiles and runs.

To be more specific template argument deduction should work since GCC 7, Clang 5, and MSVC 19.14. cppreference.com gives you the details to the compiler support.

What's next?

Do you know what a Regular or SemiRegular type is? If not, the next post to template interfaces is just the right one for you. Rule T.46 states: "Require template arguments to be at least Regular or SemiRegular.".

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

Modern C++ Concurrency in Practice

C++ Standard Library including C++14 & C++17

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

What's Inside?

140 lessons

110 code playgrounds => Runs in the browser

78 code snippets

55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.