C++20 Lambda expressions, Non-type template parameters, Constraints and Concepts

Gajendra Gulgulia
17 min readApr 1, 2023

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:

  1. What is a function pointer (or more precisely function object)?
  2. How can a function object be used as template parameter in class and function template?
  3. What are the limitations prior to C++17/20 for function objects as template parameters ?
  4. 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…

--

--

Gajendra Gulgulia
Gajendra Gulgulia

Written by Gajendra Gulgulia

I'm a backend software developer and modern C++ junkie. I run a modern cpp youtube channel (https://www.youtube.com/@masteringmoderncppfeatures6302/videos)

No responses yet