|
Boost : |
From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2004-04-13 05:23:14
"Jan Gaspar" <jano_gaspar_at_[hidden]> wrote
(snip using negative values for index in operator[] )
There are few different cases:
1. operator[] for circular_buffer iterator:
circular_buffer<int> b(10);
... fill buffer
circular_buffer<int>::iterator it = b.begin() + 5;
int x = it[-1]; // should return value b[4]
I think here negative values should be allowed here
(provided they result in valid offset).
2. operator[] for circular_buffer itself:
circular_buffer<int> b(10);
b.push_back(10);
b.push_back(20);
b.push_back(33);
int x = b[-1]; // this would return value 30, the last one
x = b[-2]; // would return 20
This would be new feature, not available in current
standard ontainers. I remember some language
(forgot name) provides it.
I personally would like to have this feature
and have it in std::vector/deque too.
Alternatively one can think about syntax as:
int x = b[boost::end - 1];
to indicate intent and catch possible bugs.
(boost::end would be tag type and operator[]
would be overloaded.)
3. incrementing/decrementing circular_buffer
iterator with automatic wrap-around:
I think this (wrap-around) should not be
allowed:
- it could hide errors
- it suggests circular-linked-list semantics
(and one can then ask for rotate())
/Pavel
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk