More Lambda Features with C++20


Lambdas in C++20 can be default-constructed and support copy-assignment when they have no state. Lambdas can be used in unevaluated contexts. Additionally, they detect when you implicitly copy the this pointer. This means a significant cause of undefined behaviour with lambdas is gone.


I want to start with the last feature of the enumeration. The compiler detects the undefined behavior when you implicitly copy the this pointer. Okay, what does undefined behavior mean? With undefined behavior, there are no restrictions on the behavior of the program, and you have, therefore, no guarantee of what can happen.

I like to say in my seminars: When you have undefined behavior, your program has catch-fire semantic. This means, your computer can even catch fire. In former days undefined behavior was described more rigorously: with undefined behavior, you can launch a cruise missile. Anyway, when you have undefined-behavior, there is only one action left: fix the undefined behavior.

In the next section, I intentionally cause undefined behaviour.

Implicitly Copy of the this Pointer

The following program captures implicitly the this pointer by copy.


// lambdaCaptureThis.cpp

#include <iostream>
#include <string>

struct Lambda {
    auto foo() const {
        return [=] { std::cout << s << std::endl; };   // (1) 
    std::string s = "lambda";
     ~Lambda() {
        std::cout << "Goodbye" << std::endl;

auto makeLambda() {                                               
    Lambda lambda;                                     // (2)                               
}                                                      // (3)

int main() {
    std::cout << std::endl;

    auto lam = makeLambda();                                
    lam();                                             // (4)                                                               
    std::cout << std::endl;


The compilation of the program works as expected, but this does not hold for the execution of the program.


Do you spot the issue in the program lambdaCaptureThis.cpp? The member function foo (1) returns the lambda [=] { std::cout << s << std::endl; } having an implicit copy of the this pointer. This implicit copy is no issue in (2), but it becomes an issue at the end of the scope. The end of the scope means the end of the lifetime of the local lambda (3). Consequently, the call lam() (4) triggers undefined behaviour.

A C++20 compiler must, in this case, write a warning. Here is the output with the Compiler Explorer and GCC.



The two missing lambdas features of C++20 sound not so thrilling: Lambdas in C++20 can be default-constructed and support copy-assignment when they have no state. Lambdas can be used in unevaluated contexts. Before I present both features together, I have to make a detour: What does unevaluated context mean?

Unevaluated Context

The following code snippet has a function declaration and a function definition. 

int add1(int, int);                       // declaration
int add2(int a, int b) { return a + b; }  // definition


add1 declares a function, but add2 defines it. This means, if you use add1 in an evaluated context such as invoking it, you get a link-time error. The critical observation is that you can use add1 in unevaluated contexts such as typeid, or decltype. Both operators accept unevaluated operands.


// unevaluatedContext.cpp

#include <iostream>
#include <typeinfo>  // typeid

int add1(int, int);                       // declaration
int add2(int a, int b) { return a + b; }  // definition

int main() {

    std::cout << std::endl;

    std::cout << "typeid(add1).name(): " << typeid(add1).name() << std::endl; // (1)
    decltype(*add1) add = add2;                                               // (2)
    std::cout << "add(2000, 20): " << add(2000, 20) << std::endl;
    std::cout << std::endl;


typeid(add1).name() (1) returns a string representation of the type and decltype (2) deduces the type of its argument.


Stateless Lambdas can be default-constructed and copy-assigned

Lambdas can be used in unevaluated contexts

Admittedly, this is quite a long title. Maybe the term stateless lambda is new to you. A stateless lambda is a lambda that captures nothing from its environment. Or to put it the other way around. A stateless lambda is a lambda, where the initial brackets [] in the lambda definition are empty. For example, the lambda expression auto add = [ ](int a, int b) { return a + b; }; is stateless.

When you combine the features, you get lambdas, which are quite handy. 

Before I show you the example, I have to add a few remarks. std::set such as all other ordered associative containers from the standard template library  (std::map, std::multiset,  and std::multimap) use per-default std::less to sort the keys. std::less guarantees that all keys are ordered lexicographically in ascending order. The declaration of std::set  on shows you this ordering behavior.

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class set;


Now, let me play with the ordering in the following example.


// lambdaUnevaluatedContext.cpp

#include <cmath>
#include <iostream>
#include <memory>
#include <set>
#include <string>

template <typename Cont>
void printContainer(const Cont& cont) {
    for (const auto& c: cont) std::cout << c << "  ";
    std::cout << "\n";

int main() {
    std::cout << std::endl;

    std::set<std::string> set1 = {"scott", "Bjarne", "Herb", "Dave", "michael"};
    using SetDecreasing = std::set<std::string, decltype([](const auto& l, const auto& r){ return l > r; })>;           // (1)
    SetDecreasing set2 = {"scott", "Bjarne", "Herb", "Dave", "michael"};
    printContainer(set2);     // (2)

    using SetLength = std::set<std::string, decltype([](const auto& l, const auto& r){ return l.size() < r.size(); })>; // (1)
    SetLength set3 = {"scott", "Bjarne", "Herb", "Dave", "michael"};
    printContainer(set3);     // (2)

    std::cout << std::endl;

    std::set<int> set4 = {-10, 5, 3, 100, 0, -25};

    using setAbsolute = std::set<int, decltype([](const auto& l, const auto& r){ return  std::abs(l)< std::abs(r); })>; // (1)
    setAbsolute set5 = {-10, 5, 3, 100, 0, -25};
    printContainer(set5);    // (2)
    std::cout << "\n\n";


set1 and set4 sort their keys in ascending order. set2, set3, and set5 do it unique using a lambda in an unevaluated context. The using keyword (1) declares a type alias, which is used in the following line (2) to define the sets. Creating the set causes the call of the default-constructor of the stateless lambda.

Thanks to the Compiler Explorer and GCC, here is the output of the program.


When you study the output of the program, you may be surprised. The special set3 which uses the lambda [](const auto& l, const auto& r){ return l.size() < r.size(); } as a predicate, ignores the name "Dave". The reason is simple. "Dave" has the same size as "Herb", which was added first. std::set supports unique keys and the keys are in this case identical using the special predicate. If I had used std::multiset, this wouldn't have happened.

What's next?

Only a few smaller features in C++20 are left. The little features include the new attributes [[likely]] and [[unlikely]], and most of volatile were deprecated.




Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, Tornike Porchxidze, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Dimitrov Tsvetomir, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, and Michael Young.


Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, and Rusty Fleming.



My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small



I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

Bookable (Online)


Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.


Contact Me

Modernes C++,


Tags: lambdas, C++20

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Interactive Course: The All-in-One Guide to C++20

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code


Today 6299

Yesterday 7646

Week 39594

Month 106260

All 7374100

Currently are 150 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments