Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用boost::bind将静态成员函数绑定到boost::函数_C++_Boost - Fatal编程技术网

C++ 如何使用boost::bind将静态成员函数绑定到boost::函数

C++ 如何使用boost::bind将静态成员函数绑定到boost::函数,c++,boost,C++,Boost,我想使用boost::bind将静态成员函数绑定到boost::函数。下面是我想做的事情(不工作)的一个例子 但对于静态成员,这显然是不正确的 你能给我解释一下如何使用boost::bind正确绑定类的静态成员函数吗?这将和普通函数绑定一样。 对于静态函数,您只需要使用它的类名来识别编译器的函数,并跳过this参数,因为静态finction绑定到类而不是对象。 下面是一个简单的例子: #include <iostream> #include <boost/function.hp

我想使用boost::bind将静态成员函数绑定到boost::函数。下面是我想做的事情(不工作)的一个例子

但对于静态成员,这显然是不正确的


你能给我解释一下如何使用boost::bind正确绑定类的静态成员函数吗?

这将和普通函数绑定一样。 对于静态函数,您只需要使用它的类名来识别编译器的函数,并跳过this参数,因为静态finction绑定到类而不是对象。 下面是一个简单的例子:

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
void test(boost::function<bool(int, int)> func)
{
    std::cout<<"in test().\n";
    bool ret = func(10, 20);
    std::cout<<"Ret:"<<ret<<"\n";
}
class Test
{
    public:
    static bool test2(int a, int b)
    {
            std::cout<<"in test2().\n";
            return a>b;
    }
};

int main()
{
    test(boost::bind(&Test::test2, _1, _2));
    return 0;
}

O/P:
in test().
in test2().
Ret:0

希望这会有所帮助。:-)

您的代码是正确的。也许您遇到了一些编译器问题?你能举出编译结果吗

您也可以尝试改用std::function和std::bind。仅用“功能”标题替换boost标题并写入

std::placeholders::_1
而不是

_1

错误消息是什么?它与绑定常规(非成员)函数相同。抱歉,我不知道如何绑定到非成员函数您的代码是正确的,应该可以正常工作。我运行了此操作,它正常工作。谢谢你提供了清晰的代码示例。嗯,我运行了自己的代码,它也很有效(正如@Demo所建议的)。我想也许昨晚看足球时喝的那些啤酒模糊了我的思维。
int main()
{
    test(boost::bind(&Test::test2, this, _1, _2));-----> Illegal
    return 0;
}
Below error will occur:
techie@gateway1:myExperiments$ g++ boost_bind.cpp
boost_bind.cpp: In function âint main()â:
boost_bind.cpp:26: error: invalid use of âthisâ in non-member function
std::placeholders::_1
_1