# predicate function

• May 25th, 2011, 09:46 PM
ChayKieu
predicate function
I bet there should be times you need to use a predicate not only with 2 parameters but more than that. I am now wondering how can I pass in an algorithm's function a predicate with 3 or more parameters.

That's the point where my knowledge about STL stops, could someone clear it up with new roads from here ?

Thank you.
• May 25th, 2011, 10:19 PM
Chris_F
Re: predicate function
Why don't you try posting an example of the situation you are talking about.
• May 26th, 2011, 07:40 AM
Eri523
Re: predicate function
I'm not really certain about your scenario either, but it looks like what you want is a functor instead of a function as the predicate. Its operator() would then take the usual parameters passed to the predicate by the STL algorithm, and any other information your functor object needs can be set up any way you want before invoking the algorithm.
• May 26th, 2011, 11:47 AM
Lindley
Re: predicate function
I don't think any of the existing STL algorithms require a predicate with more than 2 arguments.
• May 26th, 2011, 03:56 PM
ChayKieu
Re: predicate function
Thanks I always meet problems with more than 2

For example,

bool pred(T a, T b, bool c)
{
return c?a.x<b.y:a.x>b.y;
}

this is not allowed is , e.g remove_if
• May 26th, 2011, 04:13 PM
Lindley
Re: predicate function
Code:

```struct CompareDim {     bool cmpX;     CompareDim(bool compareX)         :cmpX(compareX)     {}     bool operator()(T a, T b) const     {           return cmpX ? a.x<b.y : a.x>b.y;     } }; int main() {     vector<T> myvec;     .....     myvec.erase(         remove_if(myvec.begin(),myvec.end(),CompareDim(true)),         myvec.end()); }```
Or, in C++0x lambda functions....
Code:

```int main() {     vector<T> myvec;     .....     bool cmpX = true;     myvec.erase(         remove_if(myvec.begin(),myvec.end(),[cmpX](T a, T b){ return cmpX ? a.x<b.y : a.x>b.y; }),         myvec.end()); }```
• May 26th, 2011, 04:43 PM
Eri523
Re: predicate function
Lindley, under which circumstances does remove_if() take a binary predicate?

I had my proposed code sample just ready when the notification mail about your post came in, and now that I have it I want to post it... :D It is a functor-style unary predicate that takes the "other" T and the bool as constructor parameters:

Code:

```class predicate { public:   predicate(const T &b, bool c) : m_b(b), m_c(c)   {}   bool operator()(const T &a) const   {     return m_c ? a.x < m_b.y : a.x > m_b.y;   } private:   const T &m_b;   bool m_c; };```
BTW, how does your first example work at all, as I don't see an operator() in there anywhere? Is there a third way to define predicates I don't know of yet?

Actually, I, personally, find the lambda function example even easier to understand. I'm gradually approaching comprehension of them... :)
• May 26th, 2011, 05:19 PM
Lindley
Re: predicate function
Quote:

Originally Posted by Eri523
Lindley, under which circumstances does remove_if() take a binary predicate?
...
BTW, how does your first example work at all, as I don't see an operator() in there anywhere? Is there a third way to define predicates I don't know of yet?

You're right, I goofed on the operator. And remove_if is not a good example of this predicate (perhaps std::sort would be better).
• May 26th, 2011, 06:04 PM
Eri523
Re: predicate function
Quote:

Originally Posted by Lindley
You're right, I goofed on the operator.

Ah, ok. :)

Quote:

And remove_if is not a good example of this predicate (perhaps std::sort would be better).
Indeed, for std::sort() a binary predicate makes perfect sense. I'm not familiar enough with the STL algorithms to reliably tell off the top of my head which of them require a binary predicate and which a unary one (given it requires a predicate at all).

I'm curious about what would happen when remove_if() actually would get presented a binary predicate. I somehow suppose it would throw some sort of "operator not defined" error during template instantiation, right?

Of course it would be no problem to define a functor which is both a unary and a binary predicate at the same time. This would provide allround compatibility though I have only a vague idea of what that might be useful for. (However, I don't have the slightest idea of which practical use the predicate skeleton presented by the OP in post #5 might be either.)

While it's clearly not possible to simply take the address of an overloaded function due to overload resolution ambiguity, it might be even possible to use overloaded unary and binary function-style predicates of the same name for the STL algorithms due to the way template instantiation works (at compile-time!). However, this is just wild speculation and I can't imagine any practical use for that either.
• May 26th, 2011, 08:42 PM
laserlight
Re: predicate function
Quote:

Originally Posted by Eri523
it might be even possible to use overloaded unary and binary function-style predicates of the same name for the STL algorithms due to the way template instantiation works (at compile-time!). However, this is just wild speculation and I can't imagine any practical use for that either.

Maybe such a dual purpose predicate can be used with std::transform :)

Incidentally, by "function-style predicates" I think you mean "function object predicates".
• May 27th, 2011, 05:35 AM
Eri523
Re: predicate function
Quote:

Originally Posted by laserlight
Maybe such a dual purpose predicate can be used with std::transform :)

Ah! I was looking for an example of this and std::transform() definitely is one. :) Thanks.

Quote:

Incidentally, by "function-style predicates" I think you mean "function object predicates".
Err... Unless I misunderstand you here: no. To those you mean I was referring as "functor-style". Maybe I should brush up my terminology regarding this area of C++ a bit? :o
• May 27th, 2011, 06:49 AM
laserlight
Re: predicate function
Quote:

Originally Posted by Eri523
Err... Unless I misunderstand you here: no. To those you mean I was referring as "functor-style". Maybe I should brush up my terminology regarding this area of C++ a bit?

Yes, because in C++ terminology, function object and functor are synonymous :)
• May 27th, 2011, 08:23 AM
Eri523
Re: predicate function
Quote:

Originally Posted by laserlight
Yes, because in C++ terminology, function object and functor are synonymous :)

Well, that's why I used the term "fuction-style" as opposed to "functor-style". I never wrote "function-object-style" as a third variant which of course woud've pretty much messed up the whole thing. :ehh: