Boost logo

Boost :

From: Gregory Seidman (gseidman_at_[hidden])
Date: 2001-05-28 11:03:12

Douglas Gregor sez:
} On Monday 28 May 2001 01:16 am, you wrote:
} > a[8]; // exactly 8 times
This is the problem. See below.

} > a[0, more]; // you said can be done, i believe so (comma (,) op)
} > a[0, until(15)]; // likewise can be done
} >
} > we can even have:
} >
} > a[from(0), until(10)] for people who prefer verbosity.
} >
} > Why the fuss? I want iteration to be very distinct from grouping. Too many
} > parentheses make the code quite difficult to read except for lisp/scheme
} > folks.
} >
} > Thanks,
} > Joel de Guzman
} I'm a bit over-paranoid when it comes to syntax that will compile but run in
} an unexpected manner. By allowing the first three mentioned above, it's
} _really_ easy for a user to forget the "until" and write a[0, 15] instead of
} a[0, until(15)] - and the two are totally different. Some compilers warn
} about this, but it could perhaps be considered an "embarrassment", akin to
} "vector<list<int>>" but without any compile-time error.

To make it a compile error, we do the following:

class from {
    from(int min);

class until {
    until(int max);

until more(-1); //define "infinity"

class range {
    range(const from &min, const until &max);
    explicit range(int minmax);

range operator,(const from &min, const until &max)
{ return range(min, max); }

class IforgetNow {
    operator[](const range &range);

Now there is no implicit conversion from an int to a range, and a range is
the only type the class providing [] can take in the brackets. If you try
to use a[0, 15] then it will tell you that no method matches
operator[](int). If you want to do a[15] you can do a[range(15)]
explicitly. Also, if you feel like writing pedantic code, you can use
a[range(0, 15)] to make it clear that it's a range.

} Doug

Boost list run by bdawes at, gregod at, cpdaniel at, john at