Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar? [duplicate]

The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers?

like image 549
static_rtti Avatar asked Mar 10 '13 12:03

static_rtti


People also ask

Is std :: function a function pointer?

They are not the same at all. std::function is a complex, heavy, stateful, near-magic type that can hold any sort of callable entity, while a function pointer is really just a simple pointer. If you can get away with it, you should prefer either naked function pointers or auto - bind / auto -lambda types.

What can not be done with function pointers?

2. What will we not do with function pointers? Explanation: As it is used to execute a block of code, So we will not allocate or deallocate memory.

Why do we need std :: function?

Instances of std::function can store, copy, and invoke any CopyConstructible Callable target -- functions (via pointers thereto), lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.

Is std :: function an object?

std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them.


1 Answers

std::function is designed to represent any kind of callable object. There are plenty of callable objects that cannot be represented in any way by a function pointer.

  1. A functor:

    struct foo {
      bool operator()(int x) { return x > 5; }
    };
    
    bool (*f1)(int) = foo(); // Error
    std::function<bool(int)> f2 = foo(); // Okay
    

    You cannot create an instance of foo and store it in a bool(*)(int) function pointer.

  2. A lambda with a lambda-capture:

    bool (*f1)(int) = [&](int x) { return x > y; }; // Error
    std::function<bool(int)> f2 = [&](int x) { return x > y; }; // Okay
    

    However, a lambda without a capture can be converted to a function pointer:

    The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

  3. Implementation-defined callable return values:

    bool foo(int x, int y) { return x > y; };
    
    bool (*f1)(int) = std::bind(&foo, std::placeholders::_1, 5); // Error (probably)
    std::function<bool(int)> f2 = std::bind(&foo, std::placeholders::_1, 5); // Okay
    

    std::bind's return value is an implementation-defined callable object. Only how that object may be used is specified by the standard, not its type.

like image 66
Joseph Mansfield Avatar answered Nov 11 '22 12:11

Joseph Mansfield