Jump to content
  • Advertisement
Sign in to follow this  
grill8

Question about C++ predicate less.

This topic is 3989 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

Hello all, I have a question about the C++ predicate less. Ok, so in a nutshell, C++ defines the predicate less as: template<class T> struct less : public binary_function<T, T, bool>{ bool operator()(const T& x, const T& y)const{return x<y;} }; I have now encountered a scenario where I need a differing object for the second comparison. So, I need a predicate such as: template<class T1, class T2> struct less : public binary_function<T1, T2, bool>{ { bool operator()(const T1& x, const T2& y)const{return x<y;} }; I need to know if such a predicate already exists somewhere in the C++ archives or whether I need to write my own. I just do not want to reinvent the wheel if such an object already exists. Thank you for your help, Jeremy (grill8)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Deyja
You just wrote it in 3 lines. Don't worry about it.


QFE. Toss it into your collection of code and be done with it.

Share this post


Link to post
Share on other sites
All -
I realize it is possible to simply implement it and be done with it, but it makes me uncomfortable knowing that duplicate code could be hanging around not to mention polluting the source code.
For my own sanity I just need to know if it is already implemented.

Thank you,
Jeremy (grill8)

Share this post


Link to post
Share on other sites
FWIW, I do not believe that such a predicate would already exist in the standard library. The standard library design has a strong design bias toward the concept of homogeneous sequences and so it would be unlikely that such a predicate parameterised by a different type for each argument would already exist.

There are a couple of counter examples, most notably std::iter_swap, but they're generally considered to be rather odd.

Edd

Share this post


Link to post
Share on other sites
the_edd - Thank you. That was my reasoning as well but I needed confirmation.

All -
I will go ahead and construct my own since I cannot find one already. If anyone knows any different, please lmk. This topic remains open.

Jeremy (grill8)

Share this post


Link to post
Share on other sites
Wont that then cause an ambiguous function call when T1 and T2 are the same type, if you don't explicitly specify the template parameters?

btw you stuck an extra curly brace in your OP.

Share this post


Link to post
Share on other sites
iMalc -
You are right on both accounts (shouldn't have the extra curly brace, and it would cause an ambiguous function call). The function should be named differently such as less_2param.

All -
If anyone knows any more about this please lmk.

Thanks, Jeremy (grill8)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!