C++ Insights – Lambdas
Honestly, many programmers in my classes have issues with the syntactic sugar of lambdas. Desugaring lambdas with C++ Insights helps quite often.
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 that 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 for 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:
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
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 using 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 that 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 maybe fixed soon. I think it’s worth knowing 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: 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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.
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 |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!