Search code examples
c++std-function

Wrap intro std function


So, I have no reason to use std::function for this statement. Just from what I read the following statement can be written using std::function.

How should this statement look like? Written with std::function

template <class T>
class CFooTemplate
{
public:
    typedef void (T::*FOO_STATE)(void);

Explain a little, the logic behind writing.


Solution

  • I'm not entirely sure what you mean but I'll give it a stab.

    How should this statement look like? Written with std::function

    If I understand correctly, you mean this line: typedef void (T::*FOO_STATE)(void);

    In this case, you would write typedef std::function<void()> FOO_STATE;

    This is because you're using a pointer to a (member) function. The signature of the function itself is void(), which in C++ is equivalent to void(void) as you used.

    std::function takes the raw signature (e.g. void() or void(void)) and creates a polymorphic wrapper for any function of that general signature, be it a normal function, member function, or function-like object.