Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::function with non-static member functions

i'm trying to understand a concept and an error. what's wrong with this?

class A
{
public:
    A()
    {
        std::function<void(int)> testFunc(&A::func);
    }

private:
    void func(int) {}
}

my question, is it possible to create any sort of object that is able to call a member of a specific instance where std::function acts like a member function pointer, except without the wacky type definition that can't be used as function parameters in inheriting classes. for example:

class A
{
public:
    A()
    {
         index[WM_CREATE] = &A::close;
         index[WM_DESTROY] = &A::destroy;
    }

protected:
    map<UINT msg, void (A::*)(HWND, UINT , WPARAM, LPARAM)> index;
    void close(HWND,UINT, WPARAM, LPARAM);
    void destroy(HWND, UINT, WPARAM, LPARAM);
};

class B : public A
{
public:
    B()
    {
        index[WM_CREATE] = &B::create; // error because it's not a pointer of type A::*
    }
private:
    void create(HWND, UINT, WPARAM, LPARAM);

};

I'm thinking i'm on the right track of using std::functions like so:

class A
{
public:             //         Gigantic stl error with these two
    A()             //                         |
    {               //                         V
         index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::close);
         index[WM_DESTROY] = std::function<void(HWND, UINT, WPARAM, LPARAM>(&A::destroy);
    }

protected:
    map<UINT msg, std::function<void(HWND, UINT, WPARAM, LPARAM)> > index;
    void close(HWND,UINT, WPARAM, LPARAM);
    void destroy(HWND, UINT, WPARAM, LPARAM);
};

class B : public A
{
public:                    //               and this one
    B()                    //                     |
    {                      //                     V
        index[WM_CREATE] = std::function<void(HWND, UINT, WPARAM, LPARAM)>(&B::create);
    }
private:
    void create(HWND, UINT, WPARAM, LPARAM);

};

if someone could explain what these giant cryptic errors mean and how to fix them, i would greatly appreciate it.

like image 909
FatalCatharsis Avatar asked Apr 05 '12 04:04

FatalCatharsis


People also ask

How do you call a non static member function in C++?

A class can have non-static member functions, which operate on individual instances of the class. class CL { public: void member_function() {} }; These functions are called on an instance of the class, like so: CL instance; instance.

Can static function access non static variables in C++?

Static member functions are allowed to access only the static data members or other static member functions, they can not access the non-static data members or member functions of the class.

How do you make a function not static?

But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”. So we need to create an instance of the class to call the non-static method.

Can static functions access non static members?

A static method can access only static members and can not access non-static members. A non-static method can access both static as well as non-static members.

What are non-static member functions in a class?

A class can have non-static member functions, which operate on individual instances of the class. These functions are called on an instance of the class, like so: They can be defined either inside or outside the class definition; if defined outside, they are specified as being in the class' scope.

How do you declare a non static member function?

A non-static member function may be declared virtual or pure virtual. See virtual functions and abstract classes for details. A non-static member function can be declared to take as its first parameter an explicit object parameter, denoted with the prefixed keyword this .

What types of member functions can be declared in C++?

Any function declarations are allowed, with additional syntax elements that are only available for non-static member functions: pure-specifiers, cv-qualifiers, ref-qualifiers, final and override specifiers (since C++11), and member initialization lists . 1) For an object of type X using the class member access operator

How are non-static member functions treated during overload resolution?

During overload resolution, non-static member function with a cv-qualifier sequence of class X is treated as follows: no ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X and is additionally allowed to bind rvalue implied object argument


1 Answers

I think the problem you are having is that a member function requires not only a function pointer, but a pointer to the calling object. In other words, member functions have an additional implicit argument that is the pointer to the calling object.

To set a member function to a std::function, you need to use std::bind like this:

std::function<void(int)> testFunc(std::bind(&A::func, this, _1));

This binds the this pointer of the current A instance to the function so it has the function pointer and the object instance, which is enough information to properly call the function. The _1 argument indicates that the first explicit argument will be provided when the function is called.

like image 85
Jason B Avatar answered Oct 15 '22 06:10

Jason B