We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Back to Basics: Templates in C++ - Nicolai Josuttis - CppCon 2022
Discover the fundamentals of C++ templates, from function templates to template metaprogramming, and learn how to use concepts, `auto` keyword, and more to simplify generic programming and execute code at compile-time.
- Templates are a fundamental concept in C++ that allows for generic programming.
- Function templates are used to create functions that can work with different data types.
- Template metaprogramming is a technique used to execute code at compile-time.
- Concepts are a feature introduced in C++20 that allows for specifying requirements for types in a more expressive way.
-
Template parameters can be specified using the
auto
keyword to let the compiler determine the type. -
Classes can be defined using template parameters, such as
std::vector
. - Although templates were considered expensive on compile time in the past, modern compilers have improved their performance.
- The concept of “ad hoc” requirements is introduced, where requirements are specified for specific types.
- Function templates can be used to create functions that can work with different data types, such as arithmetic operations.
-
The
size
member function is used to get the size of a container. - Concepts can be used to specify requirements for types, such as copyability.
-
The
auto
keyword can be used to let the compiler determine the type of a variable. - Function templates can be used to create functions that can work with different data types, such as printing.
- The concept of “compile-time if” is introduced, where code is executed at compile-time.
- Concepts can be used to specify requirements for types, such as supporting the “less than” operator.
- Template metaprogramming can be used to implement complex algorithms and data structures.
-
The use of
auto
and concepts can simplify the writing of generic code. -
The
std::vector
class is an example of a class that uses templates to work with different data types.