Jump to content

  • Log In with Google      Sign In   
  • Create Account


Function taking as an argument std::equal_to, std::grater etc


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Misery   Members   -  Reputation: 305

Like
0Likes
Like

Posted 18 July 2012 - 05:22 AM

Hello,

I am trying to make a function that would take some kind of comparision operator from std:: library. For example:

   std::equal_to
as one of the arguments.
However, I fail all the time. Could someone give an example of such function, that would work?

Thanks in advance,
Regards

Sponsor:

#2 mrbastard   Members   -  Reputation: 1573

Like
4Likes
Like

Posted 18 July 2012 - 05:25 AM

template <typename T>
bool doStuff(int a, int b, T comparitor)
{
	 return comparitor(a,b);
}

I'm guessing the problem you're having is that the component std functions / functors are not of the same type. Template parameters make this problem go away.

Some of the std component functions may be of related types : e.g. deriving from std::unary_function etc - so it may be possible to use normal polymorphism to work around this - I wouldn't recommend it though. TBH I'm not even sure inheriting from bases like std::unary_function is enforced by the standard.

Lastly, you could take an std::function<bool (int,int)> or similar, if you want to avoid writing templates.

Edited by mrbastard, 18 July 2012 - 05:32 AM.



#3 Matt-D   Crossbones+   -  Reputation: 1410

Like
3Likes
Like

Posted 18 July 2012 - 02:19 PM

A good place to look for examples (and inspiration) are the docs for SGI STL: http://www.sgi.com/tech/stl/
Often you can find something similar to what you're trying to accomplish. In your case std::count_if (for instance) takes a "Predicate pred" parameter, just like you want in case of std::equal_to being an argument: http://www.sgi.com/t...l/count_if.html

[source lang="cpp"]template &lt;class InputIterator, class Predicate, class Size&gt;void count_if(InputIterator first, InputIterator last, Predicate pred, Size& n);[/source]

Note that in the context of template parameters specification (the first line, the one that starts with "template") you can either use "class" (you are allowed to pass a built-in type, like "std::size_t" for "Size" or "int *" for "InputIterator", so it's a bit of a misnomer) or "typename" pretty much interchangeably (the only case where you have to use the "class" are the so-called TTPs (template-template parameters, like when you want to pass a class template "std::vector" as opposed to, say, template class "std::vector<double>"), but don't worry about it here).

Another reason I point out the docs is that they show good naming conventions for template parameters -- it's good idea to use names that express the concept, like "Predicate", instead of general names, like "T", for self-documenting code. Even though it's "just" for documentation (it won't be enforced, concepts as a type enforcement mechanism didn't get into C++11), it's pretty useful -- it allows the code reader (often you) to just quickly skim at the function signature to figure out what it does and what it wants (just like in the above "count_if" example). You can look up the concept you need by looking up the class (template) you want to use, for instance, http://www.sgi.com/t...l/equal_to.html

Another good example of using a concept name to document your requirements is std::sort, http://www.sgi.com/tech/stl/sort.html -- that's the relevant example for arguments like std::less:

[source lang="cpp"]template &lt;class RandomAccessIterator, class StrictWeakOrdering&gt;void sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);[/source]

This makes clear to the users of std::sort that they are expected to pass a particular comparator function object -- one consistent with a strict weak ordering -- not just "any" comparator.

See also:
http://www.sgi.com/t...l/functors.html
http://www.sgi.com/t...yPredicate.html

BTW, note that unary_function & binary_function are deprecated in C++11:
http://www.open-std....2010/n3145.html
http://en.cppreferen.../unary_function

Edited by Matt-D, 18 July 2012 - 02:47 PM.


#4 mrbastard   Members   -  Reputation: 1573

Like
1Likes
Like

Posted 18 July 2012 - 02:43 PM

BTW, note that unary_function & binary_function are deprecated in C++11:
http://www.open-std....2010/n3145.html
http://en.cppreferen.../unary_function


Nice to know, thanks!


#5 Misery   Members   -  Reputation: 305

Like
1Likes
Like

Posted 19 July 2012 - 01:21 AM

Thanks a lot :]
Your suggestions Gyus, did the job!

Edited by Misery, 19 July 2012 - 01:21 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS