boost-bind

Does boost::bind() copy parameters by reference or by value?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:11:00
Why does valgrind's DRD tool complaines "Conflicting load by thread ... at size 4": about such code: void SomeFunction(const int& value) { boost::bind(..., value); /* <-- complaines on this line with last backtrace function "new(int)" */ } Does boost::bind() stores values by reference or value? By value. 1 But you can make it copy by ref instead: void SomeFunction(const int& value) { boost::bind(..., boost::ref(value)); boost::bind(..., boost::cref(value)); // by const ref } 1 http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#Purpose a copy of the value of i is stored into the function

Getting return value from a boost::threaded member function?

不羁的心 提交于 2019-11-27 18:21:11
问题 I have a worker class like the one below: class Worker{ public: int Do(){ int ret = 100; // do stuff return ret; } } It's intended to be executed with boost::thread and boost::bind, like: Worker worker; boost::function<int()> th_func = boost::bind(&Worker::Do, &worker); boost::thread th(th_func); th.join(); My question is, how do I get the return value of Worker::Do? Thanks in advance. 回答1: I don't think you can get the return value. Instead, you can store the value as a member of Worker:

What is the return type of boost::bind?

安稳与你 提交于 2019-11-27 15:26:38
I want to save the "binder" of a function to a variable, to use it repetitively in the following code by exploiting its operator overloading facilities. Here is the code that actually does what I want: #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> class X { int n; public: X(int i):n(i){} int GetN(){return n;} }; int main() { using namespace std; using namespace boost; X arr[] = {X(13),X(-13),X(42),X(13),X(-42)}; vector<X> vec(arr,arr+sizeof(arr)/sizeof(X)); _bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1); cout <<

Difference between C++11 std::bind and boost::bind

余生长醉 提交于 2019-11-27 06:15:53
Is there any difference between the two? Or am I safe to replace every occurrence of boost::bind by std::bind in my code and thereby remove the dependence on Boost? boost::bind has overloaded relational operators , std::bind does not. boost::bind supports non-default calling conventions , std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind expressions ( boost::protect ), std::bind does not. (That said, one can use boost::protect with std::bind if they

Calling base class definition of virtual member function with function pointer

狂风中的少年 提交于 2019-11-26 21:48:23
问题 I want to call the base class implementation of a virtual function using a member function pointer. class Base { public: virtual void func() { cout << "base" << endl; } }; class Derived: public Base { public: void func() { cout << "derived" << endl; } void callFunc() { void (Base::*fp)() = &Base::func; (this->*fp)(); // Derived::func will be called. // In my application I store the pointer for later use, // so I can't simply do Base::func(). } }; In the code above the derived class

Can I use (boost) bind with a function template?

别说谁变了你拦得住时间么 提交于 2019-11-26 21:47:57
问题 Is it possible to bind arguments to a function template with (boost) bind? // Define a template function (just a silly example) template<typename ARG1, typename ARG2> ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2) { return arg1 + arg2; } // try to bind this template function (and call it) ... boost::bind(FCall2Templ<int, int>, 42, 56)(); // This works boost::bind(FCall2Templ, 42, 56)(); // This emits 5 pages of error messages on VS2005 // beginning with: error C2780: // 'boost::_bi::bind_t<_bi::dm

How to use boost bind with a member function

本小妞迷上赌 提交于 2019-11-26 17:21:32
The following code causes cl.exe to crash (MS VS2005). I am trying to use boost bind to create a function to a calls a method of myclass: #include "stdafx.h" #include <boost/function.hpp> #include <boost/bind.hpp> #include <functional> class myclass { public: void fun1() { printf("fun1()\n"); } void fun2(int i) { printf("fun2(%d)\n", i); } void testit() { boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) ); boost::function<void (int)> f2( boost::bind( &myclass::fun2, this ) ); //fails f1(); f2(111); } }; int main(int argc, char* argv[]) { myclass mc; mc.testit(); return 0; }

Difference between C++11 std::bind and boost::bind

 ̄綄美尐妖づ 提交于 2019-11-26 11:55:14
问题 Is there any difference between the two? Or am I safe to replace every occurrence of boost::bind by std::bind in my code and thereby remove the dependence on Boost? 回答1: boost::bind has overloaded relational operators, std::bind does not. boost::bind supports non-default calling conventions, std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind

how boost::function and boost::bind work

耗尽温柔 提交于 2019-11-26 07:53:09
问题 I dislike having magic boxes scattered all over my code...how exactly do these two classes work to allow basically any function to be mapped to a function object even if the function<> has a completely different parameter set to the one im passing to boost::bind It even works with different calling conventions (i.e. member methods are __thiscall under VC, but \"normal\" functions are generally __cdecl or __stdcall for those that need to be compatible with C. 回答1: boost::function allows

How to use boost bind with a member function

回眸只為那壹抹淺笑 提交于 2019-11-26 05:24:10
问题 The following code causes cl.exe to crash (MS VS2005). I am trying to use boost bind to create a function to a calls a method of myclass: #include \"stdafx.h\" #include <boost/function.hpp> #include <boost/bind.hpp> #include <functional> class myclass { public: void fun1() { printf(\"fun1()\\n\"); } void fun2(int i) { printf(\"fun2(%d)\\n\", i); } void testit() { boost::function<void ()> f1( boost::bind( &myclass::fun1, this ) ); boost::function<void (int)> f2( boost::bind( &myclass::fun2,