C++ Insights - Lambdas
This is a cross-post from www.ModernesCpp.com.
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 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
Experienced Compiler Engineer focused on Parallel Programming and High Performance Computing
5 年Hi Rainer, I noticed that all the indentation appears to be missing from your examples. Maybe they have tabs instead of spaces or something else. Please fix if you can find the time. Your articles are always very useful!
Entrepreneur, Leader, Architect, Full-Stack Extreme Virtuoso: Business Analysis, Cyber Security, Data Science. ITIL BPM SLM Expert bringing Modern Approaches to drive Business Processes.
5 年Yes- I love these language features but one has to be so careful to clearly understand the language implementations and usage implications - thank you Rainer!
C++ | Trading | HFT
5 年great article! thanks!) there is always something new in Lambdas.