callable entity

C++ is language of functions – global, instance or class functions. However it is not just functions that are callable. C++ provides an operator() to make even objects behave as if they were functions, affectionately known as functors, function objects to be more precise. With C++0x lambda’s have been added to callable entity’s list.

Overall here is the summary of the callable objects:

#include <algorithm>
//
//
template
int length(T (&arr)[N] )
{
    return N;
}
//
//
template
T* end(T (&arr)[N])
{
    return arr+N;
}
//
//
template
struct Comparator
{
    const Type& _comparisonObject;
    Comparator(const Type& obj) : _comparisonObject(obj)
    {}
    bool operator()(const Type& other) const
    {
        return other == _comparisonObject;
    }
};
//
//
int tobefound = 0;
//
//
bool find_function(int val)
{
    return tobefound == val;
}
//
//
int main()
{
	//
    int arr[] = {0,1,2,3,4,5,6,7,8,9};
    //  using function
    //  tobefound is global variable
    //
    //  Readability/Maintainability -- (Ok)
    //
    tobefound = 5;
    auto iterator1 = std::find_if(arr, arr + length(arr), find_function);
	//
    bool found1 = iterator1 != end(arr);
	//
	//
    //  using function_pointer
    //  uses the find_function's pointer to
    //  find the object, one can change the function used on fly
    //
    //  Readability/Maintainability -- (Ok--)
    //  function pointer can get ugly very quickly
    //
    typedef bool(*CompareFunc_t)(int);
    CompareFunc_t compareFunc1 = find_function;
	//
    auto iterator2 = std::find_if(arr, arr + length(arr), find_function);
	//
    bool found2 = iterator2 != end(arr);
	//
	//
    //  using functor (function object)
    //  comparator object stores state of what needs to be found
    //
    //  Readability/Maintainability -- (Good)
    //  can change the object that compares, plus no global state to be
    //  maintained
    //
    Comparator comparator(tobefound);
	//
    auto iterator3 = std::find_if(arr, arr + length(arr), comparator);
    bool found3 = iterator3 != end(arr);
	//
	//
    //  using lambda's
    //
    //  benefits of functor - functor class definition
    //
    //  Readability/Maintainability -- (Better)
    //
    auto iterator4 = std::find_if(arr, arr + length(arr),
                                [](int val) {return val == tobefound;});
	//
    bool found4 = iterator4 != end(arr);
	//
	//
    return 0;
}
//

The last bit of code demos the newly added functionality in C++0x, lambda. Lambda’s are un-named function objects that can maintain state (optional). They promote use of functional programming and the use of small un-named functions that we generally need with STL.

Lets look at this lambda:

[](int val) {return val == tobefound;}

[] – indicates start of a lambda, un-named local function object

(int val) – takes one parameter of type int by value

{return val == tobefound;} – defines function body. Here the variable tobefound is captured by value, for this un-named function object and it returns bool.

Here is what compiler generates for lambda’s:

	//
	int tobefound = 5;
    auto lambda1 = [tobefound](int val) {return val == tobefound;};
    //
	//
	class UnNamedLocalFunction
    {
        int localVar;
		//
    public:
        UnNamedLocalFunction(int var) : localVar(var)
        {}
		//
        bool operator()(int val)
        {
            return val == localVar;
        }
    };
	//
	//
    UnNamedLocalFunction lambda2(tobefound);
	//
    bool is5_one = lambda1(5);
    bool is5_two = lambda2(5);

Here both lambda1 and lambda2 are equivalent. Thus lambda’s are syntactic sugar. Note this is one of the vary basic usage of lambda.

This post is one of the basic post for upcoming ones on functional programming and lambdas.

Stay tuned…

Sharing Options:

Tagged , , . Bookmark the permalink.

2 Responses to callable entity

  1. Do Niczego says:

    Excellent post. I was checking constantly this blog and I am impressed! Very useful information specially the last part :) I care for such information much. I was looking for this certain info for a long time. Thank you and best of luck.

  2. Pingback: c++11 lambda — having fun with brackets! | nullptr