DEV Community

Lambda Expressions in C++

Sandor Dargo on December 19, 2018

Do you want to get a C++ question every day to get prepared for your next interview? Sign up for DailyCppInterview for free! Reading through Scott...
Collapse
 
lucasso profile image
Łukasz

comment in the following example is wrong

[this](int value) {
  return 0 < value && value < this->m_upperBound; // doesn't compile, m_upperBound is not a non-static local
}

i guess it compiles

Collapse
 
sandordargo profile image
Sandor Dargo

You are right, I forgot to remove the comment there. Thanks a lot for spotting it!

Collapse
 
raghuram profile image
github_avenger • Edited
auto widgets = std::vector<Widget> { … }; // a bunch of widgets
for (auto& widget : widgets) {
  widgets.resize(); // Should this be widget.resize() ?? in both non-lambda and lambda ways
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
visheshpatel profile image
Vishal Chovatiya

Hey Sandor,

I had also written an article on Lambda expression covering basic to somewhat advance with simple & small illustrative examples here

Beginners will see "How Lambda expression synthesize by the compiler?" and for experienced programmers, you will see "There is a Lambda function returning another Lambda function"

I have also developed a Lambda mind map which is bonus material here.

Suggestion & Comments are most welcome.

Collapse
 
pearsondaniels profile image
Pearson Daniels

Great read! Any direction on resources for learning C or C++? This got me interested.

Collapse
 
markboer profile image
Mark Boer

That depends, I took a course that used this as course material. It is extensive, but it's no pleasant read.

If you know the syntax, but want to know best practices I can highly recommend the CPPCoreGuidelines by Bjarne (creator of the language) and Herb.

Collapse
 
sandordargo profile image
Sandor Dargo

That's a very good question which is difficult to answer.
Cppreference.com is not bad. I'd say take some tutorial and stick to it. That's not how I learnt so I cannot really recommend any but here is one.

Once you learnt the syntax and the basics, read some more advanced books, like Effective Modern C++ and follow some blogs, like Fluentcpp to learn about deeper topics.

Collapse
 
seagerjs profile image
Scott Seager

While maybe not the best site to learn C++, cppreference.com is a great place to consult when you need to know how to use a particular piece of standard functionality.

Collapse
 
raghuram profile image
github_avenger • Edited

With using [&, divisor] as a capture, everything will be captured by value except for the variable that is explicitly listed.

Should that be captured by reference ??

Collapse
 
ranjithpp profile image
ranjithpp

excellent read! thanks

Collapse
 
sandordargo profile image
Sandor Dargo

Thanks for your kind words!