I’m happy to announce a new update to my book on lambda expressions! A few pages more, and what’s important is that I heavily improved the consistency and some wording. All of that, thanks to valuable input from my readers and C++ experts. And today we’ll also have a look at one wording case - what’s a functor? Is a lambda expression “translated” into a functor by the compiler?

See below.

Changes

This time the book got three new pages, so in total it’s 149 now.

Some notable changes:

  • Wording for data members, more consistency - previously I used “member fields”.
  • Wording for function objects and mention why using “functor” is not the best idea.
  • Clarification about capturing, initialisation and generated compiler code.
  • Lots of grammar fixes, typos, “a”/“the”, code issues.
  • Extended the Reference Sections.

Let’s now see an adapted excerpt from the book, where I discuss the term “functor”’

Why Not a Functor

Sometimes on my blog, but also in various C++ articles, we refer to class types with operator() as “functors”. While this term is handy and much shorter than “function object class type” it’s not 100% correct.

// Functor or a Function Object Type?
struct Printer {
    void operator()(int x) const {
        std::cout << x << '\n';
    }
};

As it appears, “Functor” comes from functional programming, and it has a different meaning than colloquially used in C++.

Quoting Bartosz Milewski on Functors:

A functor is a mapping between categories. Given two categories, C and D, a functor F maps objects in C to objects in D — it’s a function on objects.

It’s very abstract, but fortunately, we can also look at some simpler definition. In chapter 10 of “Functional Programming in C++: How to improve your C++ programs using functional techniques 1st Edition” (see @Amazon) Ivan Cukic “translates” those abstract definitions into more practical one for C++:

A class template F is a functor if it has a transform (or map) function defined on it.

Also, such a transform function must obey two rules about identity and composition.

Additionally, the term “Functor” is not present in any form in the C++ Specification (even in C++03), so it looks like it’s good to avoid it.

I recommend the following sources to read more about Functors:

The credit for this change in the book goes to Björn Fahller (@bjorn_fahller) for noticing that I might want to improve wording in the book regarding this term. Also many thanks to Andreas Fertig (andreasfertig.info)

Background of The Book

Going back to the book update… Here’s a brief description of its origin:

On my website, you can read lots of articles that describe all the essential parts of lambdas:

Have a look if you haven’t read it before:

I packed my knowledge about lambdas into a beautiful ebook, with lots of examples and additional descriptions. And just as with my book about C++17, I made it available at Leanpub. The platform makes it easy for me to publish new updates, and readers can immediately access the latest changes (at no charge). Also, the platform allows you not only to read it as PDF but also Epub, Mobi or Online.

Here’s the list of the things you can learn:

  • C++98 & C++03 - What was the motivation for the new C++ feature?
  • C++11 - Early days: You’ll learn about all the essential aspects of lambdas and several tricks you might apply. This is the longest chapter as we need to cover a lot of topics.
  • C++14 - Updates: See how to use generic lambdas and captures with an initialiser.
  • C++17 - More improvements, especially by handling this pointer and allowing constexpr. You’ll also learn how to leverage the overloaded pattern.
  • C++20 - In this chapter, you’ll see all of the new features adopted for C++20 like template lambdas and how to use them with concepts and constexpr algorithms.

Acknowledgment

This Particular update woulnd’t be possible with a valuable input from:

Thank you for your valuable feedback and discussions!

Plus I got lots of input from other Readers, Patron Discord Server and C++Polska Blog.

Thank you all!

Kindle Version

This version is also available as a Kindle book, at all stores on Amazon!

For example, here’s the US version:

https://www.amazon.com/dp/B08F2LT8YG

Your Feedback

I appreciate your feedback and support! The book has now almost 1000 readers (and only one refund)! That’s not too bad I think :)

Add your feedback/review here:
https://www.goodreads.com/book/show/53609731-c-lambda-story

You can use this comment site:
https://leanpub.com/cpplambda/feedback

Or just write a direct email to me: bartlomiej DOT filipek AT bfilipek DOT com

How to Get the Book and Join ~1250 Readers?

There are three ways:

1) Buy directly at Leanpub (-10% for Cyber Monday Only Today!):

C++ Lambda Story @Leanpub This platform also gives you a 45-day refund period!

2) Buy at Amazon, Kindle version:

C++ Lambda Story, Kindle

3) Buy together with my C++17 Book:

Buy C++17 in Detail AND C++ Lambda Story Together

4) Support me on Patreon - each Patrong gets this book for free!

Become a Patron and Get Extra Content

You can also download a free sample which contains almost 60 pages! (Available at the Leanpub Page)