TimelineCpp20CoreLanguage

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 behavior with lambdas is gone.

 TimelineCpp20CoreLanguage

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 semantics. 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 behavior.

Implicitly Copy of the this Pointer

The following program implicitly captures the this pointer by copy.

 

Rainer D 6 P2 500x500Modernes C++ Mentoring

Be part of my mentoring programs:

  • "Fundamentals for C++ Professionals" (open)
  • "Design Patterns and Architectural Patterns with C++" (open)
  • "C++20: Get the Details" (open)
  • "Concurrency with Modern C++" (starts March 2024)
  • Do you want to stay informed: Subscribe.

     

     

    // 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)                               
        return lambda.foo();
    }                                                      // (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.

    LambdaCaptureThis

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

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

    LambdaCaptureThisWarning

     

    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.

     unevaluatedContext

    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 pretty handy. 

    Before I show you the example, I must 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 cppreference.com shows you this ordering behavior.

    template<
        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"};
        printContainer(set1);
        
        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};
        printContainer(set4);
    
        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 uniquely use 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 program’s output.

     lambdaUnevaluatedContext

    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 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 was deprecated.

     

     

     

    Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.

    Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.

    My special thanks to Embarcadero
    My special thanks to PVS-Studio
    My special thanks to Tipi.build 
    My special thanks to Take Up Code
    My special thanks to SHAVEDYAKS

    Seminars

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

    Standard Seminars (English/German)

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

    • C++ – The Core Language
    • C++ – The Standard Library
    • C++ – Compact
    • C++11 and C++14
    • Concurrency with Modern C++
    • Design Pattern and Architectural Pattern with C++
    • Embedded Programming with Modern C++
    • Generic Programming (Templates) with C++
    • Clean Code with Modern C++
    • C++20

    Online Seminars (German)

    Contact Me

    Modernes C++ Mentoring,

     

     

    0 replies

    Leave a Reply

    Want to join the discussion?
    Feel free to contribute!

    Leave a Reply

    Your email address will not be published. Required fields are marked *