Tips and TricksWebinar Recap

Summer Codefest: Lambdas go Baa! [Recap]

Webinar overview: 

This presentation by product manager, David Millington, talks about the convenient way to define an anonymous function object added in C++11. This topic was chosen because while it’s extremely useful, the data we see is that there tend to be two groups of C++ developers: those who use them extensively, and those who barely use them.

When to use lambdas:

The main benefits of using lambdas are:

  • Improves readability for you or your team.
  • Anonymity makes them easier to maintain (no names needed for smaller functions/functors).
  • Localizes functions to your code.

Furthermore, lambdas are especially useful if your logic goes inside something else. These code layering problems are a nuisance to reading code—lambdas make it easier to “localize” logic.

Comparing lambdas with traditional functor

A comparison between a sort functor written in traditional structure vs a lambda.

On the left is a standard functor with structs and operators written traditionally. It works and functions just as a lambda would but it is longer and arguably more difficult to comprehend when viewed in the context of actual source code.

On the other hand, a lambda is seen as significantly shorter and easier to read. With the structure of a lambda, the code being called is emphasized directly after the functions. The syntax is also unmistakable; just look for the following method syntax:

  • [ ] – capture state
  • ( ) – function
  • { } – body of method

Skip to 18:18 of the replay to learn more about lambda syntax and how to structure inline functions.

Summary: other tips for using lambdas

Here are a couple of other things you should look out for according to the presentation:

Slide Deck Presentation

Replay

Related posts
Tips and Tricks

Getting started with how to use C++ for embedded systems in financial services

Tips and Tricks

The biggest challenges in writing C++ programs for finance and banking

News

Catching up with VA: Our most recent performance updates

Build Announcements

Visual Assist 2024.4 release post - ARM Support

Leave a Reply

%d