After I presented in my last post Template Specialization, the basics about template specialization, I dig deeper today. I want to present the partial and full specialization of a class template as a compile-time if.
Specialization of Class Templates as a Compile-Time if
After my last blog post Template Specialization, I got a few similar questions. How can you decide if a type is a given type or if two are the same? Answering these questions is easier and helps me present more theories about class template specialization. To answer these questions, I implement simplified versions of
std::is_same , and std::remove_reference. The presented techniques in this post apply class template specialization and are a compile-time if.
std::is_same is a function from the type-traits library. It returns std::true_type if both types are the same, otherwise, it returns
std::false_type. For simplicity reasons, I return
The primary template (1) returns as the default
false, when you ask for its
value. On the contrary, the partial specialization (2) that is used when both types are the same returns
true. You can use the class template
isSame on types (3) and, thanks to
decltype, on values (4). The following screenshot shows the output of the program.
You may already guess it. The class template
isSame is an example of template metaprogramming. Now, I have to make a short detour and write a few meta words.
Metafunctions and Metadata
At runtime, we use data and functions. At compile time, we use metadata and metafunctions. Quite easy, it’s called meta because we do metaprogramming, but what is metadata or a metafunction? Here is the first definition.
- Metadata: Types and integral values that are used in metafunctions.
- Metafunction: Functions that are executed at a compile time.
Let me elaborate more on the terms metadata and metafunction.
Metadata includes three entities:
- Types such as int, double, or
- Non-types such as integrals, enumerators, pointers, lvalue reference, and floating-point values with C++20
So far, I have only used types in my metafunction
Types such as the class template
isSame are used in template metaprogramming to simulate functions. Based on my definition of metafunctions,
constexpr functions can also be executed at compile time and are, therefore, metafunctions.
cannot only return a value, but it can also return a type. By convention, a metafunction returns a using via
::value, and a type using
::type.The following metafunction
removeReference returns a type as a result.
In this example, I apply the previously defined metafunction
isSame and the metafunction
removeReference. The primary template
removeReference (1) returns T using the name
type. The partial specializations for the lvalue reference (2) and the rvalue reference also return T by removing the references from its template parameter. As before, you can use the metafunction
removeReference with types (4) and, thanks to
decltype, with values (5).
decltype(a) returns a value,
decltype(b) an lvalue reference, and an rvalue reference.
Finally, here is the output of the program.
There is one trap I fall into. When you define a member function of a fully specialized class template outside the class, you must not use
Member Functions of Specialization Defined Outside the Class Body
The following code program shows the class template
Matrix, having a partial and a full specialization.
(1) declares the primary template. (2) defines the partial specialization, and (3) the full specialization of
Matrix. The member functions
numberOfElements are defined outside the class body. Line (4) is probably the non-intuitive line. When you define the member function
numberOfElements outside the class body, you must not use
template <>. Line (5) causes the instantiation of the partial, and line (6) causes the instantiation of the full specialization.
In my next post, I will write about the full specialization of function templates and their surprising interplay with functions. To make a long story short, according to the C++ Core Guidelines holds: T.144: Don’t specialize function templates.
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, Bhavith C Achar, and Marco Parri Empoli.
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,