Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) template

 

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]:

 

 

templates can be used to:

 

 

Templates make it possible to use one function or class to handle many different data types.

 

 

 

 

 

 

Examples

 

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. 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
  2. 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 [...]'
  3. C++. International Standard. ISO/IEC 14882. Second edition. Paragraph 14.1.2.
  4. 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'
  5. 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'
  6. 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'
  7. 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'
  8. 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'
  9. 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'
  10. 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'
  11. 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'
  12. 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'
  13. 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'
  14. 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'
  15. 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'
  16. 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'
  17. 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'
  18. 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'
  19. 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'
  20. 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'
  21. 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.

 

Valid XHTML 1.0 Strict