C++20 Lambda expressions, Non-type template parameters, Constraints and Concepts
In this article I will explain how to write a class and fuction template declaration which uses functions and lambda expressions as non-type template parameter.
Function as Non-Type-Template-Parameter ( NTTP henceforth) looks like below in class and function template as of C++17.
template<auto FunctionType> class Foo{
// ...
};
template<auto FunctionType>
void foo();
As can be seen the FunctionType
is completely unconstrained and in paractice such definitions are full of loopholes and vaugeness in terms of what FunctionType
should look like.
This article answers the following questions:
- What is a function pointer (or more precisely function object)?
- How can a function object be used as template parameter in class and function template?
- What are the limitations prior to C++17/20 for function objects as template parameters ?
- How can one create a robust yet readable template definition that applies constraints on class and function template accepting function objects as template parameter?
If you want to uncover the answer to all the questions, then you’re in for a treat because this detailed article…