What is a C++ function object (Function Object)? What is the function of overloading parentheses () in C++?

Before understanding overloading (), you must first understand the concept of function objects in C++.

1. What is a function object?

A FunctionObject type is the type of an object that can be used on the left of the function call operator.

That is: the function object type is the object type that can be used on the left side of the function call operator (PS: it is the parentheses ()) .
According to the documentation, several types of function objects can be summarized:

  1. function or function reference (requires implicit conversion)
  2. function pointer
  3. < functional > All function objects defined in the header file (such as less, greater)
  4. < functional > The return value of some functions in the header file

Notes
Functions and references to functions are not function object
types, but can be used where function object types are expected due to
function-to-pointer implicit conversion.

Standard library
All pointers to functions satisfy this requirement.
All function objects defined in < functional >
Some return types of functions of < functional >

In addition:
5. Lambda expressions (after C++11)
6. Objects of classes that overload the function call operator ()

In C++, among the custom comparison functions: common incoming function names, less or greater objects belong to this kind of function objects. However, something like the following does an implicit type conversion . (I didn’t look at the specific conversion process, if you have a clear message, you can leave a message)

// 传入函数名
vector<int> vec;
bool cmp(const pair<int, int>& A. const pair<int, int>& B) {
    
    
	return A.second < B.second;
}
sort(vec.begin(), vec.end(), cmp);

The following are examples of several other uses of function objects (function pointers, objects of classes that overload the function call operator ()
, lambda expressions)

#include <iostream>
 
void foo(int x) {
    
     std::cout << "foo(" << x << ")\n"; }
 
int main()
{
    
    
    void(*fp)(int) = foo;
    fp(1); // calls foo using the pointer to function
 
    struct S {
    
    
        void operator()(int x) const {
    
     std::cout << "S::operator(" << x << ")\n"; }
    } s;
    s(2); // calls s.operator()
    s.operator()(3); // the same
 
    auto lam = [](int x) {
    
     std::cout << "lambda(" << x << ")\n"; };
    lam(4); // calls the lambda
    lam.operator()(5); // the same
}

2. Overloaded function call operator () parentheses

Parentheses are function call operators.
Overloading () parentheses, that is, overloading the function call operator.

When an overloaded function of a user-defined class calls operator(), it becomes of type FunctionObject. The FunctionObject type can be called as a function, and objects of this type can be used in function call expressions:

This may be a bit obscure, but the following example should be easy to understand:

// An object of this type represents a linear function of one variable a * x + b.
struct Linear
{
    
    
    double a, b;
 
    double operator()(double x) const
    {
    
    
        return a * x + b;
    }
};
 
int main()
{
    
    
    Linear f{
    
    2, 1};  // Represents function 2x + 1.
    Linear g{
    
    -1, 0}; // Represents function -x.
    // f and g are objects that can be used like a function.
 
    double f_0 = f(0);
    double f_1 = f(1);
 
    double g_0 = g(0);
}

The class after the overloaded function call operator () is the function object (FunctionObject) class, which has the function of the function object (FunctionObject) class, and the instantiated object is the one introduced in the first section of this article.

References:

  1. https://en.cppreference.com/w/cpp/named_req/FunctionObject
  2. https://en.cppreference.com/w/cpp/language/operators
    For the second link, refer to the Function call operator section. These two documents are very well written and highly recommended

Guess you like

Origin blog.csdn.net/Sansipi/article/details/127876449