C++ boost::bind在存储时不保存部分参数
我试图进行boost::bind调用,并在boost::function中保存传递参数的值,但我偶然发现了无法解释的情况:C++ boost::bind在存储时不保存部分参数,c++,templates,boost,boost-bind,C++,Templates,Boost,Boost Bind,我试图进行boost::bind调用,并在boost::function中保存传递参数的值,但我偶然发现了无法解释的情况: #include <boost/bind.hpp> #include <boost/function.hpp> #include <memory> #include <iostream> template <class T> using callback = boost::function<void (in
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <memory>
#include <iostream>
template <class T>
using callback = boost::function<void (int, std::shared_ptr<T>)>;
class work
{
public:
int value;
};
void job_callback(int v, std::shared_ptr<int> work)
{
*work = v;
}
int main()
{
std::shared_ptr<int> a(new int(10));
boost::bind(job_callback, _1, a)(1); // compiles and works with 1 arg, *a is 1
callback<int> call = boost::bind(job_callback, _1, a); // compiles
call(2, a); // compiles and works with 2 args, *a is 2
call(3); // does not compile with 1 arg
return 0;
}
调用
boost::bind
的返回类型不是boost::function
而是存储第二个参数值的其他类型吗?如果是,那是什么?因为boost
和std
{bind,function}
产生了同样的问题。我将使用std
版本进行讨论。我认为问题在于函数对象的赋值
callback<int> call = boost::bind(job_callback, _1, a); // compiles
使用std:{function,bind}的代码
#include <functional>
#include <memory>
#include <iostream>
using namespace std;
using namespace std::placeholders;
template <class T>
using callback = std::function<void (int, std::shared_ptr<T>)>;
class work
{
public:
int value;
};
void job_callback(int v, std::shared_ptr<int> work)
{
*work = v;
}
int main()
{
std::shared_ptr<int> a(new int(10));
std::bind(job_callback, _1, a)(1); // compiles and works with 1 arg, *a is 1
//callback<int> call = std::bind(job_callback, _1, a); // compiles
function<void (int)> call = std::bind(job_callback, _1, a); //also compiles
//call(2, a); // compiles and works with 2 args, *a is 2
call(3); // does not compile with 1 arg
return 0;
}
#包括
#包括
#包括
使用名称空间std;
使用名称空间std::占位符;
模板
使用callback=std::函数;
课堂作业
{
公众:
int值;
};
无效作业回调(int v,std::共享作业)
{
*功=v;
}
int main()
{
标准::共享ptr a(新整数(10));
std::bind(job_callback,_1,a)(1);//编译并使用1参数,*a是1
//callback call=std::bind(job_callback,_1,a);//编译
函数调用=std::bind(作业回调,_1,a);//也编译
//调用(2,a);//编译并使用2个参数,*a是2
调用(3);//不使用1参数编译
返回0;
}
如果您有选择的话,也许值得尝试在c++11中使用std::bind和std::function,而不是boost版本。@TingL我已经大量使用boost并依赖于它,而且在我瞄准的一些平台上,c++11的支持是不可靠的。然而,在这种情况下,C++11 std::function和std::bind会产生相同的结果。@Marcin:您的假设是正确的。绑定将返回一个boost::function
。共享_ptr将存储在函数object中,可能值得演示调用(7,std::shared_ptr())
,原始代码也修改了*a
——在调用(2,a)
行中,a
是一条红鲱鱼。
function<void (int)> call = std::bind(job_callback, _1, a); //also compiles
#include <functional>
#include <memory>
#include <iostream>
using namespace std;
using namespace std::placeholders;
template <class T>
using callback = std::function<void (int, std::shared_ptr<T>)>;
class work
{
public:
int value;
};
void job_callback(int v, std::shared_ptr<int> work)
{
*work = v;
}
int main()
{
std::shared_ptr<int> a(new int(10));
std::bind(job_callback, _1, a)(1); // compiles and works with 1 arg, *a is 1
//callback<int> call = std::bind(job_callback, _1, a); // compiles
function<void (int)> call = std::bind(job_callback, _1, a); //also compiles
//call(2, a); // compiles and works with 2 args, *a is 2
call(3); // does not compile with 1 arg
return 0;
}