New operands???

Hello everyone,

I was thinking about a new set of operands. (maybe a potential gcc hack).

|=, |>, |<, &=, &>, &<

Walla! basically what they do is perform a comparison on the previous comparer.

e.g...

Instead of writing this...

(bar > 10 || bar == 100 && bar < 200)

you could write this!

(bar > 10 |= 100 &< 200)

What do you guys think? would it be a worthy hack?
it would be nice syntactic sugar. O:
Well, if you were doing this, you would have to replace all uses of normal operator with new operators that stored their information for later use. Like in your example:

(bar > 10 |= 100 &< 200)

would be

(true/false |= 100 ...)

and you wouldn't have any idea what it was applied to. Seems like you are just redefining how the language works.
|= bit 'or' assignment
&= bit 'and' assignment

|> that's a pipe and output redirection $ ls |>my_files cat
|< pipe, without pipe (the intermediate output is ignored, I guess)

&> redirect stdout and stderr
&< Don't know (yet)
I have always wanted more mathematical comparisons:

if (2 <= x <= 32)

I wrote a little class to help me do stuff like that, but then the compiler started complaining that I was doing something hideous (because it didn't understand that I had specifically addressed that problem)...
@ firedraco
Hmmm... wouldn't it be fairly straight forward on how to store the memory address of the last comparer?

@ne555
They could always be different symbols, but do you think the concept is worthy to implement?
@Duoas: What exactly where you doing then? I can't imagine why you'd go beyond a simple "in_range(T low, T high)" function?
You could have 2 <= x return a variable implicitly convertible to a boolean, but had information about its parameters, then with <= 32 the same thing (or just a direct boolean). Though it wouldn't work if both operands were primitive types :(
Nope. But between(), or in() may be useful.
¿What about method chaining? polygon.ccw() and .convex() and .intersects(axis)
Though it wouldn't work if both operands were primitive types :(


you can alway write a converter which will indicate the compiler you want to perform your special operation, like:
1
2
3
4
5
template <typename T>
struct value
{
T val;
}


and then overload your operators for this type:
1
2
3
4
5
6
7
8
9
10
11
template <typename T, typename V>
binop<T, V, std::less<T, V>> operator<(T const& lhs, value<V> const& rhs)
{
return binop<T, V, std::less<T, V>>(lhs, rhs.value)
}
template <typename T, typename V>
binop<T, V, std::less<T, V>> operator<(value<T> const& lhs, V const& rhs)
{
...
}
...etc

where binop is the comparison class LB talk about.
and you could write:
2 <= value(x) <= 32

But I think that although this idea is tempting it somewhat break the semantics of operators
That is pretty much what I did -- just a little more carefully. It doesn't break any operator semantics.
For a < b < c I think there is no problem but what disturb me is the use of self assignment operators(|=, &= ,...) which have an assignment semanic which is not present here.

But if you consider this as a DSL in C++ you could assume you don't break operator semantics as you redefine it...
Topic archived. No new replies allowed.