[英]std::bind parameter to member function without object
我需要將一個參數綁定到類成員函數。 像這樣:
#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);
}
但是在這種情況下,我有編譯錯誤
error: static assertion failed: Wrong number of arguments for pointer-to
-member
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.