C++ 返回std::bind的结果
我已经设法弄明白了如何使用std::bind来替换以前通过静态变量、静态函数和编译器扩展_闭包的web触发的C风格回调的一大堆代码 此代码编译并运行成功;然而,在尝试稍微改进时,我得到了一个奇怪的编译器错误C++ 返回std::bind的结果,c++,templates,c++11,C++,Templates,C++11,我已经设法弄明白了如何使用std::bind来替换以前通过静态变量、静态函数和编译器扩展_闭包的web触发的C风格回调的一大堆代码 此代码编译并运行成功;然而,在尝试稍微改进时,我得到了一个奇怪的编译器错误 #include <iostream> #include <string> #include <functional> typedef void MyFunc(std::string &); struct Master { void f
#include <iostream>
#include <string>
#include <functional>
typedef void MyFunc(std::string &);
struct Master
{
void func(std::string &s) { std::cout << "Master::func(" << s << ")\n"; }
void go();
Master() {}
private:
Master(Master const &);
};
int main()
{
Master m;
m.go();
}
void call_function( std::function<MyFunc> fp, std::string s )
{
fp(s);
}
template<typename FuncT, typename Host>
typename std::function<FuncT> my_binder(Host *h, FuncT Host::*p)
{
using std::placeholders::_1;
return std::bind(p, h, _1);
}
void Master::go()
{
std::string s("hello");
// OK
using std::placeholders::_1;
std::function<MyFunc> f = std::bind(&Master::func, this, _1);
call_function(f, s);
call_function( std::bind(&Master::func, this, _1), s );
// Undefined structure 'boost::function<void(std::string &)>'
my_binder(this, &Master::func);
// I'm hoping to end up here
// call_function( my_binder(this, &Master::func), s );
}
该代码似乎在最新的g++和clang中工作,但给出了
在另一个不支持c++11的编译器上。我的代码是否正确,即失败的编译器有错误
另外:是否有一个预先存在的模板可以替代我的文件夹?我的目标是使人们能够尽可能轻松地调用call_函数,而不必到处乱动std::placeholder等。当然,使用预处理器宏可以做到这一点,但最好使用模板函数。可能编译器无法推断模板参数,需要手动将它们添加到my_binder调用中。编译器如何提取boost::function来定义my_binder?确实在命名空间stdha中使用boost::function,您尝试过吗boost::函数而不是boost::function@MikeMB传递给模板“boost::function”的参数太多
// Undefined structure 'boost::function<void(std::string &)>'