Functional object



  • What's a functional object?



  • It is interesting to note that, strictly speaking, the term C+++ is not used functional objectI mean the term functional object♪ There's a term. function object

    I don't know if these terms are equivalent to English grammar. :

    I will use the term of the function. Simply put, this term means an object of a class that has a function call operator and can be used where the postfixed expression of the function is used.

    From C++ (20.9 Function objects)

    1 A function object type is an object type (3.9) that can be the type of the postfix-expression in a function call (5.2.2, 13.3.1.1). 231 A function object is an object of a function object type. In the places where one would expect to pass a pointer to a function to an algorithmic template (Clause 25), the interface is specified to accept a function object. This not only makes algorithmic templates work with pointers to functions, but also enables them to work with arbitrary function objects.

    The most prominent representative of the object of the function is the lymbda of expression.

    Here's a demonstration program. Global function in the first cycle ::IsEvenand in the second cycle the object of the function is the local lymbda expression with the same name. IsEvenwhich has a non-identifiable operator to call a function. Lambda's announcement conceals the name of the global function.

    #include <iostream>
    

    bool IsEven( int x ) { return x % 2 == 0; }

    int main()
    {
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for ( int x : a )
    {
        std::cout &lt;&lt; x 
                  &lt;&lt; " is " &lt;&lt; ( IsEven( x ) ? "even" : "odd" ) &lt;&lt; " number"
                  &lt;&lt; std::endl;
    }
    
    std::cout &lt;&lt; std::endl;
    
    auto IsEven = []( int x ) { return x % 2 == 0; };
    
    for ( int x : a )
    {
        std::cout &lt;&lt; x 
                  &lt;&lt; " is " &lt;&lt; ( IsEven( x ) ? "even" : "odd" ) &lt;&lt; " number"
                  &lt;&lt; std::endl;
    }
    

    }

    Discharge:

    0 is even number
    1 is odd number
    2 is even number
    3 is odd number
    4 is even number
    5 is odd number
    6 is even number
    7 is odd number
    8 is even number
    9 is odd number

    0 is even number
    1 is odd number
    2 is even number
    3 is odd number
    4 is even number
    5 is odd number
    6 is even number
    7 is odd number
    8 is even number
    9 is odd number

    Instead of the lymbda of expression, you could determine your functional object. For example, you can replace an announcement in the demonstration programme

    auto IsEven = []( int x ) { return x % 2 == 0; };

    struct
    {
    bool operator ()( int x ) const { return x % 2 == 0; }
    } IsEven;

    and the outcome of the programme would be the same.
    This last announcement defines the object by name. IsEven an unnamed structure that defines the operator of the function.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2