boost-function

C++ weird syntax spotted in Boost template parameters

核能气质少年 提交于 2019-12-04 11:20:55
问题 I was having a look at the "Function" class documentation in Boost, and stumbled across this: boost::function<float (int x, int y)> f; I must admit this syntax is highly confusing for me. How can this be legal C++ ? Is there any trick under the hood ? Is this syntax documented anywhere? 回答1: [Edit] This is an answer to the author's original, unedited question which was actually two questions. I must admit this syntax is highly confusing for me. How can this be legal C++ ? :) Is there any

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 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

When to use std::function instead of inheritance?

≯℡__Kan透↙ 提交于 2019-12-03 16:48:27
问题 In some cases std::function can replace inheritance. The following two code snippets are very similar (about the same costs when calling the function, almost the same usage in signatures and in most cases std::function does not require us to make an extra copy of A as well): struct Function { virtual int operator()( int ) const =0; }; struct A : public Function { int operator()( int x ) const override { return x; } }; Using std::function , we can rewrite this as using Function = std::function

C++ weird syntax spotted in Boost template parameters

对着背影说爱祢 提交于 2019-12-03 07:00:14
I was having a look at the "Function" class documentation in Boost, and stumbled across this: boost::function<float (int x, int y)> f; I must admit this syntax is highly confusing for me. How can this be legal C++ ? Is there any trick under the hood ? Is this syntax documented anywhere? [Edit] This is an answer to the author's original, unedited question which was actually two questions. I must admit this syntax is highly confusing for me. How can this be legal C++ ? :) Is there any trick under the hood ? Is this syntax documented anywhere ? This is perfectly legal and it's actually not too

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

Storing boost::function objects in a container

夙愿已清 提交于 2019-11-29 11:13:43
I have a vector of KeyCallback s: typedef boost::function<void (const KeyEvent&)> KeyCallback which I use to store all listeners for when a keyboard button is pressed. I can add them and dispatch the events to all callbacks with for_each , but I do not know how to actually erase a specific KeyCallback signature from my vector. For example I want something like this: void InputManager::UnregisterCallback(KeyCallback callback) { mKeyCallbacks.erase(std::find(mKeyCallbacks.begin(), mKeyCallbacks.end(), callback)); } According to boost::function documentation (see here ), there is no such thing as

Performance of std::function compared to raw function pointer and void* this?

不打扰是莪最后的温柔 提交于 2019-11-29 09:31:45
Library code: class Resource { public: typedef void (*func_sig)(int, char, double, void*); //Registration registerCallback(void* app_obj, func_sig func) { _app_obj = app_obj; _func = func; } //Calling when the time comes void call_app_code() { _func(231,'a',432.4234,app_obj); } //Other useful methods private: void* app_obj; func_sig _func; //Other members }; Application Code: class App { public: void callme(int, char, double); //other functions, members; }; void callHelper(int i, char c, double d, void* app_obj) { static_cast<App*>(app_obj)->callme(i,c,d); } int main() { App a; Resource r; r