Skip to main content

C++11 - Lambda functions

C++11 standard introduces new type of functions for C++ programming - lambda functions.

Lambda function is a anonymous (unnamed) function which you can write directly inline in the code.

Lambda function allows do some operations visible like a function without declaring it as a function (saving space and time).

C++ lambda function has following syntax:
[capture](arguments){body}
  • capture - value which should be captured outside of body class
  • arguments - list of arguments of function (the same syntax as for normal C++ functions)
  • body - body of function - statements which should be exectued when function is invoked
For better understanding take a look fo below example: Output of this example is:

In point I we are initializing vector using C++11 initializer_list

Point II is key point of our example. It demonstrate using lambda function (3rd argument of for_each function) together with with for_each algorithm of STL library in order to calculate sum of vector elements. In C++03 we had to define separate function (outside of our main function) in order to use it within some STL algorithm (ex. for_each as in this example). Thanks to lambda functions we can define such us such function without declaration - in place. It is definitely easier especially if we are going to use some small function once in our code.

In point III we are printing results of our calculations.

As you can see, lambda functions are very comfortable when you are going to use some small function only once withing your code (ex. calculate some sum).

Code of this example you can find in our GitHub account here: https://github.com/xmementoit/CppAdventureExamples/tree/master/cpp11/lambdaFunctions

Comments

Popular posts from this blog

STL - count and count_if algorithms

One of the basic and most useful STL algorithms is algorithm which can be used to count number of elements within selected container according to specified criteria. In order to do that we can use std::count or std::count_if algorithm. std::count (firstElementIterator, lastElementIterator, elementForSearch) - is function which will go through container using firstElementIterator and lastElementIterator and return number of container elements which value is equal elementForSearch std::count_if (firstElementIterator, lastElementIterator, UnaryPredicateFunction) - is function which examine range from firstElementIterator to lastElementIterator and return number of container elements which fulfill UnaryPredicateFunction criteria. UnaryPredicateFunction is function having following signature: bool functionName(const Type& a) . So, count_if returns number of elements where UnaryPredicateFunction returns true for. For better understanding let's take a...

Advanced C++ - Mutable Class Field

Today I would like to present C++ class' feature called mutable class field . Mutable class field is class' field modifier which allows to change its value even if object of the class is declared as const . Take a look at the example: Output of this example is: In point I of that example we are defining object of TestClass . Note that this object is const . As you can see in point Ia this class has three different member fields ( constInt, mutableConstInt, nonConstInt ). Those variables are public for this example, but do not worry about encapsulation here. It is just omitted for simplify this example. As you can see one of this member fields is marked as mutable class file using mutable keyword ( mutableConstInt ). Such variable can be modified even if object of class TestClass is const . It will be explained in next points of this example. In point II we are printing default values of testObject object initialized in initialization list of TestClass' default c...

Advanced C++ - Template Functions

Template function are special types of C++ function which can be used with different types (generic types). Thanks to that we can create one body of function which can be used for many different types. When we are creating template function compiler does not define any function for use at that time. Generation of funciton basing on templates are done during compilation process basing of differnt usage of template class. For better understanding take a look on following example: Output of this example is: In point I, we are defining template function which should return sum of two parameters. Without templates we need to define such function separately for each type which we should use it with (ex. separate function for int type, separate for double , separate for any other type). Thanks to template we can generate body of that function only once (as in point I) and use it to any type which is able to use body of that function. In our example we can use this template for an...