boost-bind

boost::bind with functions that have parameters that are references

佐手、 提交于 2019-12-03 04:21:44
I noticed that when passing reference parameters to boost bind, those parameters won't act like references. Instead boost creates another copy of the member and the original passed in variable remains unchanged. When I change the references to pointers, everything works ok. My question is: Is it possible to get references to work, or at least give a compiling error when it tries to use reference parameters? Timo Geusch The boost documentation for bind suggests that you can use boost::ref and boost::cref for this. I ran into similar issue expecting a bind parameter to be passed by reference

boost shared_from_this<>()

99封情书 提交于 2019-12-03 03:07:59
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. The biggest "gotcha" I've run into is that it's illegal to call shared_from_this from the constructor. This follows directly from the rule that a shared_ptr to the object must exist before you can call shared_from_this.

Help me understand this usage of boost::bind

妖精的绣舞 提交于 2019-12-02 18:45:00
Please have a look at this example posted by Johannes Schaub to sort a vector of pairs: How do I sort a vector of pairs based on the second element of the pair? std::sort(a.begin(), a.end(), boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)); I thought I do understand boost::bind, but I have trouble with this one. Question 1: the sort algorithm is expecting a predicate function as a third parameter. What I see here, is a boolean expression. What am I missing?: boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second,

Running a function on the main thread from a boost thread and passing parameters to that function

余生颓废 提交于 2019-12-01 09:45:58
问题 I have some code running in a boost thread that modifies stuff handled by the main thread which is not working and it makes sense. On android i would have the Handler which is a message queue that would execute my code on the main thread and i can pass whatever parameters i want to this handler. I want to do the same with boost so on my main thread i do the following: boost::thread workerThread(boost::bind(&SomeClass::pollService, this)); My pollService method: SomeClass::pollService() { /

Storing boost::bind functions in a std::map

只谈情不闲聊 提交于 2019-12-01 06:02:37
问题 I'm creating a bunch of functions which all effectively do the same thing: long Foo::check(long retValue, unsigned toCheck, const std::set<unsigned>& s) { auto it = s.find(toCheck); return (it == s.end()) ? -retValue : retValue; } where Foo is a class. All fairly simple so far. Now, I effectively want to create a lot of variants on this, but bound to different sets. I then want to store these in a std::map. So, using boost::bind and boost::function, do something like: void Foo::addToMap

Why is “boost::function = boost::bind(…)” creating 13 temporaries?

試著忘記壹切 提交于 2019-12-01 02:58:53
I have some pretty basic test code. I have a class that just logs all operations on it. I bound it to a boost::function object like this: void Function(const Foo&) { printf("Function invoked\n"); } // ... boost::function<void(void)> func; { Foo f; printf("\nConstructing function\n"); func = boost::bind(&Function, f); printf("Construction complete\n\n"); } I expect that the function object contains a copy of f . So creating at least one copy is mandatory. However, I find that I get 13 temporaries. Output is: Constructing function Foo::Foo(const Foo&) Foo::Foo(const Foo&) Foo::Foo(const Foo&)

Why is “boost::function = boost::bind(…)” creating 13 temporaries?

蹲街弑〆低调 提交于 2019-11-30 22:59:25
问题 I have some pretty basic test code. I have a class that just logs all operations on it. I bound it to a boost::function object like this: void Function(const Foo&) { printf("Function invoked\n"); } // ... boost::function<void(void)> func; { Foo f; printf("\nConstructing function\n"); func = boost::bind(&Function, f); printf("Construction complete\n\n"); } I expect that the function object contains a copy of f . So creating at least one copy is mandatory. However, I find that I get 13

Perform argument substitution on nested boost::bind without composition

落爺英雄遲暮 提交于 2019-11-30 15:29:21
Suppose I have a function which takes a nullary functor as an argument: void enqueue( boost::function<void()> & functor ); I have another function which takes an int and does something internally: void foo( int a); I would like to nest, but not compose, these together so that I get a functor with the signature: boost::function<void(int)> functor Which when called with a value - say 4 - performs the following: enqueue( boost::bind(&foo, 4) ) My first attempt was the following: boost::function<void(int)> functor = boost::bind(&enqueue, boost::bind(&foo,_1)) This fails because bind performs

If ampersands aren't needed for function pointers, why does boost::bind require one?

自古美人都是妖i 提交于 2019-11-30 06:56:00
I've always believed that function pointers don't require an ampersand: Do function pointers need an ampersand Yet, every example I've seen of using boost::bind shows one, and my compiler - in most situations - gives a typically inscrutable error message if it's omitted. synchronize(boost::bind(&Device::asyncUpdate , this, "ErrorMessage")); // Works synchronize(boost::bind(Device::asyncUpdate , this, "ErrorMessage")); // Fails Am I wrong in assuming that boost::bind 's first parameter is basically function pointer? Function pointers don't need it, member function pointers do. Device:

How to use boost::bind in C++/CLI to bind a member of a managed class

为君一笑 提交于 2019-11-29 09:25:35
问题 I am using boost::signal in a native C++ class, and I now I am writing a .NET wrapper in C++/CLI, so that I can expose the native C++ callbacks as .NET events. When I try to use boost::bind to take the address of a member function of my managed class, I get compiler error 3374, saying I cannot take the address of a member function unless I am creating a delegate instance. Does anyone know how to bind a member function of a managed class using boost::bind? For clarification, the following