Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize `std::function` with a member-function?

Tags:

c++

c++11

I am trying to learn std::function and here's my code:

#include <iostream>
#include <functional>

struct Foo {
    void print_add(int i){ 
      std::cout << i << '\n'; 
    }
};

typedef std::function<void(int)> fp;

void test(fp my_func)
{
  my_func(5);
}

int main(){
    Foo foo;
    test(foo.print_add);
    return 0;
}

Compiler Error:

 error: cannot convert 'Foo::print_add' from type 'void (Foo::)(int)' to type 'fp {aka std::function<void(int)>}'
     test(foo.print_add);

How can I make this work, i.e how can I pass a member function as a parameter?

like image 533
Kam Avatar asked May 30 '14 19:05

Kam


2 Answers

print_add is a non-static member function of foo, which means it must be invoked on an instance of Foo; hence it has an implicit first argument, the this pointer.

Use a lambda that captures the foo instance and invokes print_add on it.

Foo foo;
test([&foo](int i){ foo.print_add(i); });

Another option is to use std::bind to bind the foo instance:

test(std::bind(&Foo::print_add, &foo, std::placeholders::_1));

Live demo

like image 82
Praetorian Avatar answered Oct 18 '22 04:10

Praetorian


You need an object to call a non-static member function. Hence, when you want to get a std::function you have several options:

  • make the method static
  • bind an object to the function via lambda
  • store the object in the std::function
  • pass the object when the std::function is called

#include <iostream>
#include <functional>

struct A {
    int i = 42;
    int get() const { return i; }    
    
    static int get_static() { return 0; }
};

struct A_functor {
    A a;    
    int operator()() const { return a.get(); }
};

int main() {    

    // static method
    std::function<int()> f1 = &A::get_static;
    std::cout << f1() << "\n";

    // bind an object to the function via lambda
    A a;
    std::function<int()> f2 = [&a](){ return a.get(); };
    std::cout << f2() << "\n";

    // store the object in the std::function
    std::function<int()> f3 = A_functor{};
    std::cout << f3() << "\n";
    // or 
    std::function<int()> f4 = [a = A()](){ return a.get(); };
    std::cout << f4() << "\n";

    // pass the object when the std::function is called
    std::function<int(A&)> f5 = &A::get;
    std::cout << f5(a) << "\n";
    // or 
    std::function<int(A*)> f6 = &A::get;
    std::cout << f6(&a) << "\n";
    
}
like image 37
463035818_is_not_a_number Avatar answered Oct 18 '22 04:10

463035818_is_not_a_number