$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-users] [Boost.CircularBuffer] begin()/end() arithmetic not working out quite right
From: David Baird (dhbaird_at_[hidden])
Date: 2009-06-10 19:01:54
Hi,
Firstly, thanks for the work on a circular buffer.  This is very
useful since many of my applications require it.  I am having a
problem though...
I am using Boost 1.38.0.  When I call push_back(), the iterator math
makes it appear that begin() moves backwards and end() remains
constant.  (Based on other STL libraries, I expect that begin() will
remain constant and end() will keep advancing as I call push_back()).
In other words, this assertion fails (but I expect it to succeed):
    circular_buffer<int> buf(8);
    circular_buffer<int>::iterator a;
    circular_buffer<int>::iterator b;
    a = buf.begin();
    buf.push_back(1);
    b = buf.begin();
    assert(a == b); // Fails!!
Also, this code fails too (but I expect it to succeed):
    circular_buffer<int> buf(8);
    circular_buffer<int>::iterator a;
    circular_buffer<int>::iterator b;
    a = buf.end();
    buf.push_back(1);
    b = buf.end();
    assert(a != b); // Also fails!
Below is a full example that you can compile and try out:
#include <boost/circular_buffer.hpp>
#include <stdio.h>
int
main ()
{
    typedef boost::circular_buffer<int> buf_type;
    // Instead of "end" moving ahead, "begin" is moving backwards when //
    // using "push_back".
    {
        buf_type buf1(1024);
        buf_type::iterator a;
        buf_type::iterator b;
        a = buf1.end();
        buf1.push_back(1);
        buf1.push_back(2);
        b = buf1.end();
        printf ("%d\n", a == b);
        // >>>
        // got: 1
        // expected: 0
        printf ("%d, %d\n", a-buf1.begin(), b-buf1.begin());
        // >>>
        // got: 2, 2
        // expected: 0, 2
    }
    {
        buf_type buf2(1024);
        buf_type::iterator c;
        buf_type::iterator d;
        c = buf2.begin();
        buf2.push_back(1);
        buf2.push_back(2);
        d = buf2.begin();
        printf ("%d\n", c == d);
        // >>>
        // got: 0
        // expected: 1
        // (i.e. "begin()" is not still pointing to first item!!
        // This is incorrect, isn't it?)
        printf ("%d, %d\n", buf2.end()-c, buf2.end()-d);
        // >>>
        // got: 0, 2
        // expected: 2, 2
    }
    return 0;
}
Thanks,
David