overloading relational operator > and >=

Hi,

Suppose I have a vector class implemented and I would like to add the operator overloading of > and >= operators... from an earlier post of mine, I came to know that the return type should be BOOL for these ==, >, <, >=, <= etc...
However, what I require is something of this sort,

1
2
3
4
5
vec<int> v(5); // creates vector of length 5 with values 0;
v[0] = 0; v[1] = 1; v[2] = 2; v[3] = 3; v[4] = 4;
// what i would like is,
vec<bool> vv = (v > 2);
// vv[0] = 0; vv[1] = 0; vv[2] = 0; vv[3] = 1; vv[4] = 1; 


Is it admissible (or accepted) to return a VEC<BOOL> instead of BOOL? Mostly this will be utilized without creating the vector. For example, rather than creating a vector of bools where vv > 2, I would prefer the indices where this condition is satisfied... If this operator overloading is performed, then I could just use,

1
2
3
vec<int> idx = find(v > 2);
// here find is implemented to handle vec<bool> types and it returns the index...
// idx[0] = 3; idx[1] = 4; 


thank you.
It's pretty ugly, IMO. I can't say it's wrong to do it, it's just people don't expect relational operators to behave like that.
yes, I thought so. Thank you. My intention to facilitate operation on vectors and matrices with equal ease as Matlab comes in the way at times.. :)
Last edited on
I would recommend making a member function or free function to do that.

The thing with operator> and the like is that whatever these operators return, programmers
expect to be convertible to bool so that operator> can be used in if() checks, etc.
vec<BOOL> would need to be implicitly convertible to bool which may or may not be a
problem, assuming you can come up with a reasonable definition (ie, true if at least one
element is true?)
I'm not completely sure that I'm following this but let me explain my take on it. Consider this line:
vec<int> idx = find(v > 2);

You could write a function, find, to return a vector or whatever, although that's kind of nuts compared to the more familiar finds. The thing about the line is that the argument, v > 2, would be evaluated before calling find. You'd have to pass a predicate function object so that it could be tested against each element in v.

Also, you could store iterators rather than just indexes.
Topic archived. No new replies allowed.