Object-Oriented Programming

C++ Templates

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Generic Programming

A programming language paradigm where algorithms are written in terms of types to-be-specified-later and then instantiated when needed for specific types of the parameters

  • One of many paradigms supported by C++, along with procedural programming (functions) and object-oriented programming
  • Replaces the need for variant types (one variable can store many different types of data, but typically not at the same time)
  • Replaces the need for the use of inheritance

C++ Templates

  • A feature of the C++ programming language that supports generic programming
  • Can write code that works with multiple data types without having to duplicate the code for each type.
  • Templates can be applied to both functions and classes
  • Code generated from templates can be heavily optimized
  • Much of the power and flexibility of the C++ Standard Library comes from the use of C++ templates

Function Template

  • template keyword
  • Template parameters enclosed in angle brackets
  • typename keyword used to declare the template parameter

Instantiating Function Templates

  • Compiler determines T from the types of parameters in the call
  • Instantiates, (i.e., generates) a separate function for each type
  • Can also explicitly declare the template parameter value (typically a type)
  • Can expand as much as needed to new types

Non-Type

  • Template parameters do not have to be types
  • When a template is instantiated with specific types or values, the compiler generates a specialized version of the template code for those types or values

Class Templates

Fibonacci

C++ Template Fibonacci Sequence

Template Disadvantages

  • Compiler needs access to function or method definitions to instantiate, leading to use in include file
  • Instantiation for each type may lead to code bloat
  • Code may become difficult to understand
  • Error messages when types with the wrong interface are used

C++ Concepts

  • First introduced as "C++0x Concepts" in C++11, but were removed
  • Concepts Lite were merged into C++20

C++ Concepts and Errors

Extension Points

  • Lambda functions passed to std::function fields/data members
  • Direct inheritance from a Base class
  • Inheritance from Handler used by a Base class

Observations

  • Lambda functions with capture are very flexible and quick to implement in code
  • Lambda functions passed to function pointer parameters are very fast
  • However, lambda functions, when passed to std::function (that allows a non-empty capture), are very slow

C++ Templates

  • Using C++ Templates with lambda functions is very fast
  • The compiler can inline lambda functions in templates
  • No use of slow std::function
  • How std::sort() works
  • How about as extension points?

Template Function Extension

Template Class Extension

Template Class Extension

Speed of Parameter Types

Extension Points

  • Lambda functions to std::function fields/data members
  • Direct inheritance from a Base class
  • Inheritance from Handler used by a Base class
  • C++Templates for classes with lambda functions

C++ Templates as Extensions Points

  • Lots of flexibility
  • Allows the use of the capture
  • Code is (potentially) inlined
  • All the advantages of lambdas to std::function, without the speed disadvantage
  • One Drawback: Have to declare in the include file (.hpp) as the compiler needs access to the definitions

Extension Points

  • Lambda functions to std::function fields/data members
  • Direct inheritance from a Base class
  • Inheritance from Handler used by a Base class
  • C++Templates for classes with lambda functions