It is pretty interesting how the compiler deduces the types for the template arguments. To make it short, you get most of the time the type you expect. The rules do not only apply to function templates (C++98) but also to
auto (C++11), to class templates (C++17), and concepts (C++20).
C++ has supported function template argument deduction since its beginning. Here is a short recap.
Function Template Argument Deduction
Let me invoke a function template
int and double
In this case, the compiler deduces the template arguments from the function arguments. C++ Insights shows that the compiler creates a fully specialized function template for
int (1) and for
The process of template type deduction, such as in this case, produces most of the time the expected type. It is pretty enlightening to analyze this process deeper.
Template Type Deduction
When deducing the template type, three entities come into play: T, ParameterType, and expression.
Two types are deduced:
ParameterType can be a
- Reference (&) or Pointer (*)
- Universal Reference (&&)
expression can be an lvalue or an rvalue having. Additionally, the lvalue or rvalue can be a reference, or
The easiest way to understand the template type deduction process is to vary the
ParameterType is a Value
Taking the parameter by value is probably the most used variant.
expris a reference, the reference is ignored
=> newExpris created
If the parameter type is a reference or a universal reference, the constness (or volatileness) of
expr is respected.
ParameterType is a Reference (&) or Pointer (*)
For simplicity, I use a reference. The analogous argumentation holds for a pointer. Essentially, you get precisely the result you expect.
expris a reference, the reference is ignored (but added at the end).
- The expr matches the
ParameterTypeand the resulting type becomes a reference. This means,
const intbecomes a
const int&becomes a
ParameterType is a Universal Reference (&&)
expris an lvalue, the resulting type becomes an lvalue reference.
expris an rvalue, the resulting type becomes an rvalue reference.
Admittedly, this explanation was pretty technical. Here is an example.
I define and use a function template taking its argument by value (1), by reference (2), and by universal reference (3).
Thanks to C++ Insights, I can visualize the type deduction of the compiler.
- (1): Both calls of
funcValuecause the same instantiation of the function template. The deduced type is an
- (2): Calling the function
const int&gives the type
- (3): Using the function
funcUniversalReferencegive an lvalue reference or an rvalue reference.
There is one interesting fact when you invoke the function
funcValue with a C-array. The C-array decays.
Decay of a C-array
Taking a C-array by value is remarkable.
When you invoke the function template
funcValue with a C-array, the C-array decays to a pointer onto its first element. Decay has many facets. It is applied when a function argument is passed by value. Decay means that an implicit conversion function-to-pointer, array-to-pointer, or lvalue-to-rvalue is applied. Additionally, the reference of a type T and its const-volatile qualifiers are removed.
Here is the screenshot of the program from C++ Insights.
This essentially means that you don’t know the size of the C-array.
But there is a trick. Taking the C-array by reference and pattern matching on the type and the size on the C-array gives you the size of the C-array:
The function template
funcArraySize deduces the size of the C-arrays. I gave, for readability reasons, the C-array parameter the name
arr: std::size_t funcArraySize(T (&arr)[N]). This is not necessary, and you can write
std::size_t funcArraySize(T (&)[N]). Here are the internals from C++ Insights.
Finally, the output of the program:
When you understand template type deduction, you essentially understand
auto type deduction in C++11.
auto Type Deduction
auto type deduction uses the rules of template type deduction.
To remind you, these are the essential entities of template type deduction:
auto means that you have to regard
auto as the replacements for
T and the type specifiers of
auto as the replacements for the
ParameterType in the function template.
The type specifier can be a value (1), a reference (2), or a universal reference (3).
Let’s try it out and change the previous program
templateTypeDeduction.cpp and use
auto instead of function templates.
When you study the resulting types in C++ Insights, you see that they are identical to the types deduced in the program
auto decays when it takes a C-array by value.
The New pdf-Bundle is Ready: C++20 Coroutines
C++17 makes type deduction more powerful. First, automatic type deduction is possible for non-type template parameters, and second, class templates can also deduce their arguments. In particular, class template argument deduction makes the life of a programmer much more straightforward.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, and Bhavith C Achar.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,