Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
24 November 2022
This is the second of four episodes in a mini series on templates. The aim of the series is to help you understand some relatively complex template code.
In this episode we will see how to "overload" templates, more specifically, we will see two real world examples:
- The first is a sort function which has a general implementation, but also a specialized version for the type char.
- The second is a vector implementation that both has a general implementation for any type, and a specialization for booleans,
which make the vector much more space efficient.
- Finally, we will see an example of how to make C++ tell us what type a template parameter represent.
Links:
* Description on template specialization: https://en.cppreference.com/w/cpp/language/template_specialization
* Deduction rules for templates: https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
* Introduction Presentation on move semantics: https://youtu.be/Bt3zcJZIalk?list=PLHTh1InhhwT4TJaHBVWzvBOYhp27UO7mI
* The Result class is inspired from C++23's expected: https://en.cppreference.com/w/cpp/header/expected
* Our list of scheduled C++ trainings: https://training.kdab.com/portfolio/modern-cpp/
0:00 Introduction
0:33 sort - template specialization of functions
2:26 "std::expected" from C++23 - template specialization of classes
5:42 the catch - specialization on void
8:28 template specialization - in a bit more details
15:00 printing the deduced type
The example code showcased in this video is available here: https://github.com/KDAB/kdabtv/tree/master/Qt-Widgets-and-more/template-mini-series/episode2
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Learn Rust
In collaboration with our partners Ferrous Systems, KDAB provides a variety of introductory and advanced training courses for the Rust language.
Learn more
Learn Modern C++
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
Learn more