• Advertisement
Sign in to follow this  

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.

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

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


Link to post
Share on other sites
Advertisement
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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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.)

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.

Share this post


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

  • Advertisement