Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-08-07 15:51:52


Corwin Joy wrote:

> > It doesn't matter what the Standard says about it:
> > you can deduce that
> >
> > i,j forward and output implies &*i == &*j
> >
> > provided the RHS is well defined AND i,j are non-const lvalues.
>
> Can you give a more rigorous proof of this.

        No, but I think can give a counter example :-(

        struct caching_iterator<class Iterator>
        {
                Iterator i;
                static bool flag;
                static typename Iterator::value_type vcache[2];
                static Iterator icache;
                typename Iterator::value_type &operator*(){
                        *(icache)=vcache[flag]; // write cache
                        flag = !flag;
                        icache=i;
                        vcache[flag]=*i;
                        return vcache[flag];
                }
                ...
        };

Here, i==j iff i.i == j.i, ++i means ++i.i etc.
But * alternates between two caches, globally so:

        assert(i==i); // == must be equivalence relation!
        assert(&*i != &*i); //!!!!

Here &*i is not equal to itself, but *i is always equal to *j when i==j.

        The class above is probably? a conforming
iterator (adaptor). It works by delaying the write operation,
and writing to the container _every_ access. Yet there are two locations
with different addresses that always hold the correct value when you
look at them -- although the container itself is one access out of date.

If this is conforming, we've got problems, because direct operations
on the container will not work as expected: you'd be surprised if

        p = a + i;
        *p = 10;
        cout << a[i]

printed something other than 10? Of course, we could have
a container that ONLY provided iterator based operations,
and used this kind of caching iterator.

-- 
John (Max) Skaller, mailto:skaller_at_[hidden] 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk