Sign in to follow this  

Purpose of std::function

This topic is 1889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the purpose of this class template?

I suppose it simplifies the syntax for function pointers. But does it give you anything else but syntactic sugaring?

Share this post


Link to post
Share on other sites
Constalks is right. If you need to pass a callback to a piece of code, make sure it accepts an std::function, and then you can use it with function pointers or with functors of any type. This is particularly useful if you want the pass a member function of a particular object as a callback. Edited by Álvaro

Share this post


Link to post
Share on other sites
I think it's worth noticing [font=courier new,courier,monospace]std::function[/font] is correctly mapped even when capture lists are used with lambdas.
Function pointers cannot as far as I recall , and sure [i]interfaces [/i]cannot.
So they're really the only "complete" way to deal with lambdas, a thing which I consider to be a bit ugly, but that's it.

Share this post


Link to post
Share on other sites
Thanks for the answers. There is a case I am specifically interested in, but I couldn't find out if it is now possible. I want to use closures with callbacks. Take this example, with a tree walker and a callback for every node in the tree:
[CODE]class Tree {
Walk(void (*callback)(Node*));
};[/CODE]
It is common to also have an extra argument of the type "void *" that will be forwarded to the callback function. I think this is a typical case that a closure would be used, is it now possible?

Something like:[CODE]
void Callback1(Node*, int);
void Callback2(Node*, int, float);

Tree *t;
auto cb1 = std::bind(Callback1, _1, 77);
t->Walk(cb1);
auto cb2 = std::bind(Callback1, _1, 77, 1.0f);
t->Walk(cb2);
[/CODE]If possible, what is the recommended way to implement this?

Share this post


Link to post
Share on other sites
If you make [i]Tree::Walk[/i] take an [i]std::function<void(Node *)>[/i], you can then do [i]t->Walk([](Node *n){Callback1(n, 77);})[/i]. The benefit, as stated above, is that you can pass anything that is callable with a [i]Node[/i]-pointer and have no return value: a function pointer, a closure and an old-style function object, and your code doesn't have to know or take special action for the different types of callable objects. Edited by Brother Bob

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1352987704' post='5001222']
If you make [i]Tree::Walk[/i] take an [i]std::function<void(Node *)>[/i], you can then do [i]t->Walk([](Node *n){Callback1(n, 77);})[/i]. The benefit, as stated above, is that you can pass anything that is callable with a [i]Node[/i]-pointer and have no return value: a function pointer, a closure and an old-style function object, and your code doesn't have to know or take special action for the different types of callable objects.
[/quote]

I see, it is that simple! This is something I should be able to use to great advantage.

Share this post


Link to post
Share on other sites
Sign in to follow this