07 Mar 2017
Tags:
C++
Update: See also the next post, which adds a fourth idiom and mitigates some of the criticisms in the conclusion of this post.
The STL code for squaring the positive values, included the following lines to create the intermediate result t1 containing the positive values of the original list a:
more ...
03 Mar 2017
Tags:
C++
The post presenting the failure case for iterators contrasted this simple Python list comprehension:
more ...
25 Feb 2017
Tags:
C++
My last post, highlighting the differences between C++ lambdas and the lambdas in languages based upon functional programs, went more deeply into the weeds than I had intended. I realized too late that it cried out for examples. The rules for declaring free variables (the member variables of the anonymous class) in C++ lambdas can seem contorted but most uses of them fall into one of a few simple types. In this post, I want to present three simple use cases of C++ lambdas, each demonstrating a different way to use free variables.
more ...
23 Feb 2017
Tags:
C++
Update: The next post provides recipes for common use cases of lambdas. If parts of this post seem too abstract, you might find the examples in the next post clarify things.
STL iterators and algorithms really can’t be discussed without also talking about lambdas. Although the STL design predates the introduction of lambdas by almost two decades, the introduction of lambdas in C++ 2011 improved the usability of STL algorithms substantially. I don’t have statistics but I presume that the STL algorithms now receive substantially more use due to the greater simplicity of passing callbacks as lambdas rather than instances of named classes. Lambdas are purely a matter of convenience—they introduce nothing to the language that you couldn’t do before—but by making common use cases more convenient, they effectively extend the range of use cases where calling a standard algorithm makes sense.
more ...