C++20 Lambda extensions: Lambda default constructors

Gajendra Gulgulia
8 min readMar 19, 2023

C++20 Introduced a series of upgrades to lambda expressions. One of them was generic lambda with template syntax, which was covered in my past article.

In this article, I will cover another upgrade: Default constructuctible lambdas.

1. Default construction: Very short background

In C++ objects are default constructible if they satisfy certain conditions. The set of conditions vary and I’ll not go into all the details of what they are as it will be out of the scope of this article. Consider the Person class with default constructor in line 3. (All other remaining details can be ignored for now)

class Person{
public:
Person() = default; //default constructor
Person(std::uint32_t age, std::string name):
age_{age}, name_{name}
{ /*empty body */ }

std::string getName() const { return name_;}
std::uint32_t getAge() const { return age_; }

void setAge(const std::uint32_t age) {age_ = age;}
void setName(const std::string& name){name_ = name;}
private:
std::uint32_t age_{};
std::string name_{};
};

The presence of default constructor allows client code to initialize an object without passing parameters to constructor.

--

--