# Using std::bind2nd with my own functions

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

## Recommended Posts

Just for fun I was trying to write a simple tree class, and since I didn't code any iterators for it, I have a member find_node function for which I need std::binder2nd's functionality. The function is basically like this: bool find_node(bool (*func)(const node_attribute&), node_ptr& out_ptr); Obviously bind2nd can only give me new function objects, so I tried bool find_node(std::unary_function< node_attribute, bool > func, node_ptr& out_ptr); but in the latter case how do I call the function denoted by the function object? Or is there another way? Please tell me if mre info is required, also. Thanks! [smile]

##### Share on other sites
std::binary/unary_function is used to make functional objects adaptable functions. They only contain nested type aliases, there are not polymorphic types either.

This is what you want:

template < typename Predicate >bool find_node(Predicate cmp, node_ptr& out_ptr);

Now you can use it with any C++ callable entity, free-functions, static member functions, bound non-static member functions, functional obects, etc, etc.

##### Share on other sites
Thanks. I would never have imagined it would be that simple [smile].

##### Share on other sites
When you need a hint like this about how to work with standard library stuff, usually you can get it from looking at how standard library pieces work with each other. :) (See std::for_each for example.)

##### Share on other sites
Quote:
 Original post by ZahlmanWhen you need a hint like this about how to work with standard library stuff, usually you can get it from looking at how standard library pieces work with each other. :) (See std::for_each for example.)

I actually did, I had looked at find_if's definition in stl_algo.h and sure enough it had a template< typename _Predicate >. As I wrote before, I didn't imagine you could template any callable entity so I thought there must be some black magic there [smile]. Pity I didn't even try it out though; but anyway thanks for the tips, guys.