Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
template is a keyword for templates.
Templates 'provide direct support for generic programming in the form of programming using types
as parameters' [1].
A template can take parameters [2]:
- Type parameters
- Value parameters
- Template parameters
templates can be used to:
Templates make it possible to use one function or
class to handle many different data types.
-
There is no semantic difference between class and typename in a template-parameter [3,4]: 'template <class T>' and 'template<typename T>' are synonymous [5]
-
Use templates to express algorithms that apply to many argument types [6]
-
Use templates to express containers [7]
-
When defining a template, first design and debug a non-template version; later generalize by adding parameters [8]
-
Templates are type-safe, but checking happens too late [9]
-
When designing a template, carefully consider the concepts
assumed for its template arguments [10]
-
If a class template should be copyable, give it a non-template copy constructor and a non-template copy assignment [11]
-
If a class template should be movable, give it a non-template move constructor and a non-template move assignment [12]
-
A virtual function member cannot be a template member function [13]
-
Define a type as a member of a template only if it depends on all the class template's arguments [14]
-
Use function templates to deduce class template argument types [15]
-
Overload function templates to get the same semantics for a variety of argument types [16]
-
Use argument substitution failure to provide just the right set of functions for a program [17]
-
Use template aliases to simplify notation and hide implementation details [18]
-
There is no seperate compilation of templates: #include template definitions in every translation unit that uses them [19]
-
Use ordinary functions as interfaces to code that cannot deal with templates [20]
-
Seperately compile large templates and templates with nontrivial context dependencies [21]
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.1, page 665: 'Templates provide direct support for generic programming in the form of programming using types as parameters
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 25.2, page 722: 'A template can take parameters: Type parameters [...], Value parameters [...], Template parameters [...]'
- C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 14.1.2.
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 25.2, page 722: 'A template argument is defined to be a type argument by prefixing it with typename or class. The result of using either is completely equivalent'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[3] template <class T>' and 'template<typename T>' are synonymous'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[1] Use templates to express algorithms that apply to many argument types'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[2] Use templates to express containers'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[4] When defining a template, first design and debug a non-template version; later generalize by adding parameters'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[5] Templates are type-safe, but checking happens too late'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[6] When designing a template, carefully consider the concepts (requirements) assumed for its template arguments'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[7] If a class template should be copyable, give it a non-template copy constructor and a non-template copy assignment'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[8] If a class template should be movable, give it a non-template move constructor and a non-template move assignment'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[9] A virtual function member cannot be a template member function'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[10] Define a type as a member of a template only if it depends on all the class template's arguments'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[11] Use function templates to deduce class template argument types'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[12] Overload function templates to get the same semantics for a variety of argument types'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[13] Use argument substitution failure to provide just the right set of functions for a program'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[14] Use template aliases to simplify notation and hide implementation details'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[15] There is no seperate compilation of templates: #include template definitions in every translation unit that uses them'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[16] Use ordinary functions as interfaces to code that cannot deal with templates'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 23.8, page 698: '[17] Seperately compile large templates and templates with nontrivial context dependencies'
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.
