Back to Basics: Lambdas from Scratch - Arthur O'Dwyer - CppCon 2019
Why Stack Overflow recommends it
Stack Overflow users link it when explaining how lambdas and captures work under the hood, and to back up examples about capture semantics and generic lambdas.
Recommended by 4 people across 4 different months, 2020 to 2024.
What it is
A CppCon 2019 Back to Basics talk by Arthur O'Dwyer that builds up C++ lambdas from first principles, starting from C functions and moving through overloading, function templates, member functions, C++11 lambdas and C++14 generic lambdas. It also covers C++17 and C++2a changes and contrasts lambdas with std::function and std::bind.
What people said
Recommends Crazor · 2020-08 · Stack Overflow · on “How to choose iterator direction at runtime”
I'd like to show what I came up with after @Botje mentioned 'polymorphic lambdas'. I recently watched CppCon 2019: Arthur O'Dwyer “Back to Basics: Lambdas from Scratch” [this video] and I'm not quite sure if Arthur's 'Generic Lambdas' are what @Botje means, but the following code works just fine and is rather elegant IMHO: #include <iostream> #include <vector> void iterate(bool forward, std::vector<int> v) { auto l = [](auto start, auto end){ for (auto it = start; it != end; ++it) { std::cout << *it << " -> " << *std::next(it) << std::endl; } }; if (forward) { l(v.begin(), v.end()); } else { l(v.rbegin(), v.rend()); } std::cout << std::endl; } int main() { std::vector<int> v{1, 2, 3, 4, 5}; itera
Recommends IlCapitano · 2020-11 · Stack Overflow · on “Why does this Y Combinator using code fail to compile?”
@LostArrow Functors are structs/classes that usually have only a few members that need to be copied. In lambdas they're usually the captured references (basically pointers), which are cheap to copy. I suggest looking into how lambdas work under the hood, this CppCon talk [this video] is a good starting point.
Recommends asevindik · 2021-08 · Stack Overflow · on “Why capture lambda does not working in c++?”
I am playing with lambda expressions in C++, and I have tried a few things to see the outcome. I actually watched the video in CppCon Back to Basics: Lambdas from Scratch - Arthur O'Dwyer - CppCon 2019 [this video] @21:47 and started to play with lambdas. As an example, I've tried this: #include <iostream> using namespace std; int g = 10;//global var 'g' //creating lambda auto kitten = [=] () {return g+1;}; auto cat = [g=g] () {return g+1;}; // main int main() { g = 20;//modifying global variable 'g' cout<<"kitten: "<<kitten()<<"cat: "<<cat()<<endl; return 0; } Output of the above code is: kitten: 21cat: 11 In the above example: [g=g] means capture a data member whose name is g and whose type is
Recommends Garrick · 2024-01 · Stack Overflow · on “Handle enter on ftxui menu example”
If you're new to FTXUI it uses a lot of lambdas. If your uncomfortable with lambdas, I found this talk helpful in working with this library. [this video]
Cites Richard · 2022-04 · Stack Overflow · on “MSVC vs GCC & Clang Bug while using lambdas”
I was trying out an example presented at CppCon [this video] that uses lambdas . And to my surprise the program don't compile in gcc and clang(in either C++14 or C++17) but compiles in msvc. This can be verified here [LINK] . For reference, the example code is as follows: #include <stdio.h> int g = 10; auto kitten = [=]() { return g+1; }; auto cat = [g=g]() { return g+1; }; int main() { g = 20; printf("%d %d\n", kitten(), cat()); } What is the problem here(if any) and which compiler is right? Note that the code is an exact copy-paste from their official presentation slide.