std::bind 参数到没有对象的成员函数

ten*_*ta4 0 c++ stdbind std-function

我需要将一个参数绑定到类成员函数。像这样的东西:

#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);
}
Run Code Online (Sandbox Code Playgroud)

但在这种情况下我有编译错误

error: static assertion failed: Wrong number of arguments for pointer-to
-member
Run Code Online (Sandbox Code Playgroud)

use*_*670 5

std::bind不会产生成员函数指针,但它可以产生一个std::function稍后可以使用的对象:

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