tenta4
tenta4

Reputation: 313

std::bind parameter to member function without object

I need to bind one parameter to class member function. Something like this:

#include <functional>
#include <iostream>

struct test
{
    void func(int a, int b)
    {
        std::cout << a << " " << b << std::endl;
    }
};

int main(int argc, char** argv)
{
    typedef void (test::*TFunc)(int);
    TFunc func = std::bind(&test::func, 1, std::placeholders::_1);
}

But in this case I have compilation error

error: static assertion failed: Wrong number of arguments for pointer-to
-member

Upvotes: 0

Views: 2297

Answers (1)

user7860670
user7860670

Reputation: 37488

std::bind does not yield a member function pointer, but it can produce a std::function object that you can use later:

::std::function< void (test *, int)> func = std::bind(&test::func, std::placeholders::_1, 1, std::placeholders::_2);
test t{};
func(&t, 2);

Upvotes: 6

Related Questions