boost-bind

Boost threads - passing parameters by reference

这一生的挚爱 提交于 2019-12-04 12:16:43
My application has a section that resembles the following code void SomeClass::OtherMethod(std::vector<std::string>& g) { g.pushback("Something"); } void SomeClass::SomeMethod() { std::vector<std::string> v; boost::thread t(boost::bind(&SomeClass::OtherMethod,this,v) t.join(); std::cout << v[0]; //Why is this empty when the vector created on stack } I wanted to know why the vector v is empty when the vector is created on the stack and it works when it is created on the heap. I was expecting the above code to work since the vector remains in scope even when it is created on the stack. Bind

C++ generic factory with multiple constructor signatures?

放肆的年华 提交于 2019-12-04 10:20:00
Has anyone ever combined the classic generic factory by Andrei Alexandrescu (page 208 of Chapter 8 in Modern C++ Design ) with the 'multifunction' capabilities of Boost.TypeErasure ? That is, the flexibility to have several creator function signatures that vary with respect to number and type of parameters (but still have the same return type and are known at compile time). In other words, how to combine this slightly simplified generic Factory: #include <map> #include <utility> #include <stdexcept> template <class AbstractProduct, typename IdentifierType, typename ProductCreator> class

boost::bind and class member function

醉酒当歌 提交于 2019-12-04 10:15:45
问题 Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e, int x) { std::cerr << "x is " << x << std::endl; std::cerr << "e is " << e << std::endl; } struct foo { std::vector<int> v; void calc(int x) { std::for_each(v.begin(), v.end(), boost::bind(func, _1, x)); } void func2(int e, int x) { std::cerr << "x is " << x << std::endl; std::cerr << "e is " << e << std::endl; } }; int main() { foo f; f.v.push_back(1); f.v.push

Am I reinventing the wheel with this trivial method call forwarding class?

老子叫甜甜 提交于 2019-12-04 07:30:13
I just found myself creating a class template <typename T> struct invoker { void operator()(T& it) const {it();} }; so I could pass an invoker<foo> to something (which isn't under my control) which wants to call invoker<foo>::operator()(foo&) on it repeatedly with different foo instances, to get it to forward those calls to the foo 's foo::operator()() method. I know it's only a few lines, but this seems like the sort of thing which is probably already provided for by STL's functional, or boost::bind somehow. Except I can't see the trick, if there is one. (I'm sure I'm not the first person to

How to use boost::bind with non-copyable params, for example boost::promise?

不羁的心 提交于 2019-12-04 02:57:44
Some C++ objects have no copy constructor, but have move constructor. For example, boost::promise. How can I bind those objects using their move constructors ? #include <boost/thread.hpp> void fullfil_1(boost::promise<int>& prom, int x) { prom.set_value(x); } boost::function<void()> get_functor() { // boost::promise is not copyable, but movable boost::promise<int> pi; // compilation error boost::function<void()> f_set_one = boost::bind(&fullfil_1, pi, 1); // compilation error as well boost::function<void()> f_set_one = boost::bind(&fullfil_1, std::move(pi), 1); // PS. I know, it is possible to

How do you declare an extern “C” function pointer

感情迁移 提交于 2019-12-04 01:57:31
So I have this code: #include "boost_bind.h" #include <math.h> #include <vector> #include <algorithm> double foo(double num, double (*func)(double)) { return 65.4; } int main(int argc, char** argv) { std::vector<double> vec; vec.push_back(5.0); vec.push_back(6.0); std::transform(vec.begin(), vec.end(), vec.begin(), boost::bind(foo, _1, log)); } And receive this error: return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]); .............................................................^ %CXX-E-INCOMPATIBLEPRM, argument of type "double (* __ptr64 )(double) C" is incompatible with

How to parse a mathematical expression with boost::spirit and bind it to a function

こ雲淡風輕ζ 提交于 2019-12-03 17:28:24
I would like to define a function taking 2 arguments double func(double t, double x); where the actual implementation is read from an external text file. For example, specifying in the text file function = x*t; the function should implement the multiplication between x and t , so that it could be called at a later stage. I'm trying to parse the function using boost::spirit. But I do not know how to actually achieve it. Below, I created a simple function that implements the multiplication. I bind it to a boost function and I can use it. I also created a simple grammar, which parse the

What is the difference between boost::bind and boost::lambda::bind?

半世苍凉 提交于 2019-12-03 14:49:32
I can see that there are two different bind libraries for Boost, one "standalone", that can be used by including boost/bind.hpp , and another by including boost/lambda/bind.hpp . What's the difference between these two? Have a look at the explanation here: http://boost.org/doc/libs/1_46_0/doc/html/lambda/s08.html#id2143701 They have overlapping functionality but with semantic differences, they can't be used interleaved. 来源: https://stackoverflow.com/questions/5202974/what-is-the-difference-between-boostbind-and-boostlambdabind

boost shared_from_this<>()

天大地大妈咪最大 提交于 2019-12-03 11:37:54
问题 could someone summarize in a few succinct words how the boost shared_from_this<>() smart pointer should be used, particularly from the perspective of registering handlers in the io_service using the bind function. EDIT: Some of the responses have asked for more context. Basically, I'm looking for "gotchas", counter-intuitive behaviour people have observed using this mechanism. 回答1: The biggest "gotcha" I've run into is that it's illegal to call shared_from_this from the constructor. This

boost::bind and class member function

夙愿已清 提交于 2019-12-03 06:10:15
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e, int x) { std::cerr << "x is " << x << std::endl; std::cerr << "e is " << e << std::endl; } struct foo { std::vector<int> v; void calc(int x) { std::for_each(v.begin(), v.end(), boost::bind(func, _1, x)); } void func2(int e, int x) { std::cerr << "x is " << x << std::endl; std::cerr << "e is " << e << std::endl; } }; int main() { foo f; f.v.push_back(1); f.v.push_back(2); f.v.push_back(3); f.v.push_back(4); f.calc(1); return 0; } All works fine if I use func()