Re: Proposal: More flexible comparisons
Jonathan Lee wrote:
I think the following simple approach will provide great flexibility in conditions.
...
if(1<x<4)
While we're on the topic, I wish there was an "in" operator, $B":(B. As in
set-theory membership. Then you might be able to write something like
if (x $B":(B (1,4)) { } // if (x in (1,4)) { }
Also while we're at it, unions, intersections, subset,...
No joke. That would be awesome.
--Jonathan
But you can do it yourself. You can define a class Interval with a
consturctor that takes two ends (or four classes: OpenInterval,
ClosedInterval, OpenClosedInterval, ClosedOpenInterval), and then define
an operator, say, equality. Here is pseudo code:
template<class value_type> struct Interval_t
{
value_type from, to;
Interval_t(value_type f, value_type t) : from(f), to(t) {}
};
template<class T> Interval_t<T> Interval(T f, T t)
{
return Interval_t<T>(f, t);
}
template<class T> bool operator ^ (T t, Interval_t<T> const& i)
{
return t > i.from && t < i.to;
}
int main()
{
bool in = 4 ^ Interval(1, 10));
bout out = 444 ^ Interval(1, 10));
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask