June 2, 2023

Function Objects

Posted on June 2, 2023  •  3 minutes  • 583 words

In C++, a function can be treated as an object using function pointers or function objects (also known as functors). Function objects are objects that can be invoked like functions.

They provide a way to encapsulate a function within an object, allowing it to be passed around, stored in data structures, or used in algorithms that require callable entities.

A functions can be treated as first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned from functions. This flexibility allows us to create function objects, which are objects that act like functions.

To create a function object, you can define a class or struct with an overloaded operator(). This operator is known as the “function call operator” and allows instances of the class to be invoked as if they were functions. The operator() can be defined with any number of parameters and a return type, depending on the desired behavior of the function object.

Syntax

return_type operator()(arguments)
{
    // body
    ....
    ....
    return value;
}

Example

class Sub
{
public:
    double operator()(double a, double b)
    {
        return a - b;
    }
};

In the code above, the Sub class defines a function object. The operator() function is defined within the class and allows instances of the Sub class to be invoked like functions. In the main() function, an instance of Sub is created, and it’s used to add two numbers by invoking it with the parentheses (2, 3).

int main()
{
    Sub s;
    double res = s(90,20);
    std::cout << "Result : " << res << std::endl;

    return 0;
}

Full code


#include<iostream>

class Adder
{
public:
    double operator()(int num1, int num2)
    {
        return (num1 + num2);
    }
};

class Sub
{
public:
    double operator()(double a, double b)
    {
        return a - b;
    }
};

int main()
{
    Adder a;
    double res = a(45,50);
    std::cout << "Result 1 : " << res << std::endl;

    Sub s;
    double res1 = s(90,20);
    std::cout << "Result 2 : " << res1 << std::endl;
    
    return 0;
}

Let’s explore deeper into function objects in C++.

Function objects can also be used with standard library algorithms like std::sort or std::find_if. For example, you can define a function object that checks if a given number is even and then use it with std::find_if to find the first even number in a collection.

#include <iostream>
#include <vector>
#include <algorithm>

// Function object class
class IsEven {
public:
    bool operator()(int value) const {
        return value % 2 == 0;
    }
};

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    IsEven isEven;
    
    // Finding the first even number using the function object
    auto it = std::find_if(numbers.begin(), numbers.end(), isEven);
    
    if (it != numbers.end()) {
        std::cout << "First even number: " << *it << std::endl;
    } else {
        std::cout << "No even number found." << std::endl;
    }
    
    return 0;
}

In this example, the IsEven class defines a function object that checks if a given integer is even by performing the modulo operation. The operator() function returns true if the value is even, and false otherwise. The function object is then used with std::find_if to find the first even number in the numbers vector.

Function objects provide a way to customize behavior in a flexible and reusable manner. They are commonly used in various scenarios, such as sorting with custom criteria, filtering elements, or transforming data. Function objects can be handy when you need to encapsulate a specific behavior or algorithm in a reusable and callable entity.

Follow me

I work on everything coding and share developer memes