Object-Oriented Programming

C++ Template Extension Points

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Extension Points

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

Comments

  • lambda functions with capture are very flexible and quick to implement in code
  • lambda functions passed to function pointers parameters are very fast
  • However, lambda functions when passed to std::function (that allows a non-empty capture) is 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

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