C++ Insights - Lambdas

Contents[Show]

Honestly, many programmers in my classes have issues with the syntactic sugar of lambdas. Desugaring lambdas with C++ Insights helps quite often.

 05 lambdas

Lambdas

Lambdas in C++ seems to be the most interesting new language feature. My opinion is based on all the issue reports and emails I got for C++ Insights so far.

They allow us to capture variables in different ways. Due to their lightweight syntax, it’s easy to create new functions like objects. In case, you never heard of it, lambdas are essentially classes with a call operator. The lambda body we provide is then the body of the call operator. For starters that should be it. Actually lambdas are one of the reasons I created C++ Insights. It can show this transformation which, at least for me, makes it easier to explain. But see for yourself:

int main()
{
  char c{1};
  
  auto l = [=] () { return c; };

  return l();
}

Capture Options

Things get more interesting if we start talking about the capture options. A variable which is captured by reference becomes a reference member of the lambdas class. Have a close look at the signature of the call operator. It says const. However, I can still modify x. It’s just a reference and not a const reference as you can see in C++ Insights:

int main()
{
  char x{1};
  
  auto l = [&] () { 
    x = 1;
  };
}

Things change if we capture by copy. Then, the captured variable is a copy. Now the const matters, we can’t modify x without adding mutable to the lambda definition:

int main()
{
  char c{1};
  
  auto l = [=] () { return c; };
  
  auto l2 = [=] () mutable { 
    c = 1;
    
    return c;
  };
}

Knowing that a class is behind a lambda enables us to think a bit more about things, like for example the size of a lambda. Copy captures increase the size by their size plus eventually padding. To save some bytes the ordering of the captures matters as you can see here:

int main()
{
  char c;
  int x;
  char b;
  
  auto l = [=] () mutable { 
    c = 1,
    x = 2;
    b = 3;
  };
  
  static_assert(sizeof(l) == sizeof(int)*3);
  
  auto l2 = [=] () mutable { 
    x = 2;
    c = 1,
    b = 3;
  };
  
  static_assert(sizeof(l2) == sizeof(int)*2);
}

However, please note that the order of the members in the class is unspecified.

Another thing to watch out is lambdas containing captures by reference. The variables these references point to, need to be valid when the lambda is invoked otherwise you end up with a dangling reference. Here is such an example:

auto Lambda()
{
  int x{};
  
  return [&]{ return x; };
}

int main()
{
  Lambda()();
}

I think it becomes pretty clear with a look at C++ Insights:

__lambda_5_10 Lambda()
{
  int x = {};
    
  class __lambda_5_10
  {
    int & x;
    public: inline /*constexpr */ int operator()() const
    {
      return x;
    }
    
    public: __lambda_5_10(int & _x)
    : x{_x}
    {}
    
  } __lambda_5_10{x};
  
  return __lambda_5_10;
}


int main()
{
  Lambda().operator()();
}

You already knew that? Excellent! You are either careful or use copy captures because then you are safe? Well, let’s see.

How about this nice copy capture example:

auto Lambda()
{
  int  y{};
  int* x{&y};

  return [=]{ return x; };
}

int main()
{
  auto x = Lambda()();
}

Here we capture only by copy, but we still return something on the stack. The pointer we copy-capture is still a pointer pointing to something on the stack which is no longer valid after we left Lambda.

Deeper Insights

At this point we covered templates, variadic templates, fold expressions, constexpr if and lambdas. We have seen how C++ Insights can show us the internals or the details. How about to use them all together? Like this:

#include <string>
#include <type_traits>

template <typename... Ts>
std::string stringify(Ts&&... args)
{
  auto convert = [](auto&& arg) {
    using TT = std::remove_reference_t<decltype(arg)>;
    if constexpr(std::is_same_v<TT, std::string>) {
      return arg;
    } else if constexpr(std::is_array_v< TT >) {
      return std::string{arg};
    } else {
      return std::to_string(arg);
    }
  };
  
  std::string ret{};
  auto concat = [](std::string& dst, auto&& arg) {
    dst += arg;
  };
  
  (..., concat(ret, convert(args)));
   
  return ret;
}

int main()
{
  std::string hello{"hello "};
  auto s = stringify("A ", hello, 2, " ", "C++ Insights");

  printf("%s\n", s.c_str());
}

Here, we have a variadic template which contains two lambdas. The first one (convert) does the conversion from anything into a string. This is similar to what we had in the previous post. The second one concatenates all arguments. Then in the line (..., concat(ret, convert(args))); all arguments get expanded by the fold expression and with the use of the comma operator. Neat and reasonably small, at least in my opinion. How does this code look internally? Have a look at C++ Insights. With all the templates, the code gets a little too big to show it here.

I’d like to mention one caveat at this point. The two lambdas convert and concat are generic lambdas. Internally they have a call operator which is a template. This is how we can have the auto parameter. Sadly, C++ Insights does not show this part yet. It’s on the list and may be fixed soon. I think it’s worth to know this.

Amazing what we can do, right? I really like the way we can write code using the latest standard. I’m also happy to offer you my training services to let you benefit from my knowledge.

I’d like to thank Rainer for the opportunity to share information about C++ Insights on his popular blog!

Have fun with C++ Insights. You can support the project by becoming a Patreon or of course with code contributions.

This is the last post of the series here. I hope you enjoyed reading it and took something from it. Now, its up to you to explore more about C++ Insights and the language itself.

I am Fertig.

Andreas

 

 

Thanks a lot to my Patreon Supporters: Paul Baxter,  Meeting C++, Matt Braun, Avi Lachmish, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Dilettant, Marko, Ramesh Jangama, G Prvulovic, Reiner Eiteljörge, Benjamin Huth, Reinhold Dröge, Timo, and Abernitzke.

Thanks in particular to:  TakeUpCode 450 60

 

Get your e-book at Leanpub:

The C++ Standard Library

 

Concurrency With Modern C++

 

Get Both as one Bundle

cover   ConcurrencyCoverFrame   bundle
With C++11, C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.  

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

 

Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 600 pages full of modern C++ and more than 100 source files presenting concurrency in practice.

 

Get your interactive course

 

Modern C++ Concurrency in Practice

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

educative CLibrary

Based on my book "Concurrency with Modern C++" educative.io created an interactive course.

What's Inside?

  • 140 lessons
  • 110 code playgrounds => Runs in the browser
  • 78 code snippets
  • 55 illustrations

Based on my book "The C++ Standard Library" educative.io created an interactive course.

What's Inside?

  • 149 lessons
  • 111 code playgrounds => Runs in the browser
  • 164 code snippets
  • 25 illustrations
Tags: lambdas

Add comment


Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 1698

All 2308230

Currently are 129 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments