我可以看到Boost有两个不同的绑定库,一个是"独立的",可以通过包含使用boost/bind.hpp,另一个通过包含使用boost/lambda/bind.hpp.这两者有什么区别?
为什么valgrind的DRD工具抱怨"线程加载冲突...大小为4":关于这样的代码:
void SomeFunction(const int& value)
{
boost::bind(..., value); /* <-- complaines on this line
with last backtrace function "new(int)" */
}
Run Code Online (Sandbox Code Playgroud)
boost :: bind()是否按引用或值存储值?
首先,我想为冗长的帖子道歉.我希望尽可能彻底.
我已经在这个问题上坚持了几天,并且关于正确使用boost::packaged_task具有输入参数的函数的信息很少令人惊讶.
boost::asio::io_service::strand由于各种原因偶尔会排队等候.在查看boost :: futures之后,我们决定使用boost :: packaged_task完全符合我们的需要.但是,打包任务的实现似乎存在错误.
似乎packaged_task有几个不同的模板可供选择:
packaged_task<R>packaged_task<R()>packaged_task<R(ArgTypes)>为了确保我正确使用该功能,我开始很简单; 使用boost :: futures页面上的简单示例作为起点.从那里,我创建了四个简单的函数:
std::string 返回,没有参数.std::string 返回,带参数.std::string ans("forty two");
int int_no_params()
{
return 42;
}
int int_with_params(int param)
{
return param;
}
std::string string_no_params()
{
return std::string("forty two");
}
std::string string_with_params(std::string & param) // Have tried both with and without …Run Code Online (Sandbox Code Playgroud) 所以我想创建一个像这样的函数:
void proxy_do_stuff(boost::bind return_here)
{
return_here(); // call stuff pased into boost::bind
}
Run Code Online (Sandbox Code Playgroud)
我可以称之为:
proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));
Run Code Online (Sandbox Code Playgroud)
怎么办这样的事情?
我还是 boost::bind 的新手,现在正在移植一个 2 年前在 2009 年编写的程序,看到下面的编译错误。任何解决方法的想法将不胜感激。
提取的cpp文件:
class ClassA {
private:
cNamespace::Bounds bounds_msg_;
void boundsHandler(const PublisherPtr& p) {
p->publish(bounds_msg_);
}
void funcA() {
node_->advertise<cNamespace::Bounds>("bounds", 10,
boost::bind(&ClassA::boundsHandler, this, _1)); // <---- Line 445
}
};
Run Code Online (Sandbox Code Playgroud)
CMake 时出错:
/home/userA/ClassA.cpp:445: instantiated from here
/usr/include/boost/bind/bind.hpp:313: error: no match for call to ‘(boost::_mfi::mf1<void, ClassA, const PublisherPtr&>) (ClassA*&, const ros::SingleSubscriberPublisher&)’
Run Code Online (Sandbox Code Playgroud)
环境:Ubuntu 10.10,g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
可能没有必要,但功能API的参考advertise是在这里,或者:
template<class M >
Publisher advertise (const std::string &topic,
uint32_t queue_size,
const SubscriberStatusCallback &connect_cb,
const SubscriberStatusCallback …Run Code Online (Sandbox Code Playgroud) 我可以boost::bind(mycallback, this, _1, _2)跨C代码使用吗?
更新
简短的回答是否定的,boost bind不会返回一个函数指针,它可以在C代码中调用,但是一个函子(带有重载()运算符的C++对象)请参见下面的答案.
调用嵌套std::bind表达式时遇到问题.以下代码演示了此问题.它无法使用libc ++进行编译,但可以使用boost:
#define BOOST 0
#if BOOST
#include <boost/function.hpp>
#include <boost/bind.hpp>
using boost::function;
using boost::bind;
#else
#include <functional>
using std::function;
using std::bind;
using std::placeholders::_1;
#endif
int sum(int a, int b) { return a+b; }
// works
template <typename F>
int yeah(F f, int c)
{
return f(c);
}
// breaks with libc++
template <typename F>
int nope(F f, int c)
{
return bind(f, c)();
}
// fixes the problem
template <typename F>
int fix1(F f, int c)
{ …Run Code Online (Sandbox Code Playgroud) 我需要将距离函数传递给模板.因此我使用boost :: function和boost :: bind.但我不明白我必须通过课程距离:
template<class DataType, class Point, class Distance>
class CoverTree
{
Distance distance;
...
public:
CoverTree(const Distance& distance) : max_level(default_max_level), min_level(default_max_level), distance(distance) {}
...
}
Run Code Online (Sandbox Code Playgroud)
模板作者的示例如下所示:
float euclidian(const std::vector<float>& p1, const std::vector<float>& p2)
{
...
}
int main(int argc, char** argv)
{
CoverTree<float, std::vector<float>, float (*const)(const std::vector<float>&, const std::vector<float>&)> tree(&euclidian);
...
}
Run Code Online (Sandbox Code Playgroud)
现在这是我的主要内容:
int main(int argc, char** argv)
{
AllData myData;
boost::function<float (const vector<Frame>::const_iterator&, const vector<Frame>::const_iterator&)> j_dist;
j_dist = boost::bind(&AllData::jaccard_distance, myData, _1, _2);
myData.AddData("C:\\...");
cout<<j_dist(myData.DATAx.begin()+20, …Run Code Online (Sandbox Code Playgroud) 我有一个派生类,从中绑定了一个我没有在此类中重写的虚函数,因此我希望调用父类中的一个。
它与 boost (1.55) 配合得很好,但如果我从 C++11 切换到 std::bind,它会拒绝使用
错误 C2100:非法间接寻址 1> 功能(1152):请参阅函数模板实例化 '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator ()( _Wrapper &) const' 正在编译 1> with 1> [ 1> _Rx=bool, 1> _Pmf_t=bool (__thiscall Base::* )(void), 1> _Farg0=Base, 1> _V0_t=std::_Nil, 1> _V1_t=std::_Nil, 1> _V2_t=std::_Nil, 1> _V3_t=std::_Nil, 1> _V4_t=std::_Nil, 1> _V5_t=std::_Nil, 1> =std: :_Nil, 1> _Wrapper=派生 1> ]
这是最小代码
class Runnable { virtual bool Run() =0;};
class Base : public Runnable { bool Run() {/*do stuff*/ return true;}};
class Derived : public Base {};
Derived d;
std::function<bool()> …Run Code Online (Sandbox Code Playgroud) Visual Studio 2019 的最新 16.6 更新删除了std::plus::result_type、std::minus::result_type和相关的 typedef。(它们在 C++17 中已弃用,并在 C++20 中删除。)代码的大大简化版本如下所示:
template <typename FF>
struct function_wrapper {
function_wrapper(FF func, const std::string& name) : func_(func), name_(name) { }
int operator()(int i1, int i2) const { return func_(i1, i2); }
// ... other stuff ...
FF func_;
std::string name_;
};
template <typename FF>
int use_function(const function_wrapper<FF>& func, const std::pair<int, int>& args) {
return func(args.first, args.second);
}
funcWrapper<boost::function<int(int, int)>> plus_func(std::plus<int>(), "plus");
std::cout << use_function(plus_func, std::make_pair<int, int>(1, 2)) << std::endl; …Run Code Online (Sandbox Code Playgroud) boost-bind ×10
c++ ×10
boost ×5
c++11 ×2
boost-asio ×1
boost-lambda ×1
c ×1
c++03 ×1
c++20 ×1
libc++ ×1
libev ×1
stdbind ×1
templates ×1
valgrind ×1