Strange bug with iterators

Discussion in 'C++' started by Juha Nieminen, May 18, 2012.

  1. I have this piece of (C++11) code:

    //----------------------------------------------------------------------
    #include <iterator>

    template<typename Iter_t>
    struct RangeWrapper
    {
    Iter_t mBegin, mEnd;
    RangeWrapper(Iter_t b, Iter_t e): mBegin(b), mEnd(e) {}
    Iter_t begin() { return mBegin; }
    Iter_t end() { return mEnd; }
    };

    template<typename Container_t>
    RangeWrapper<typename Container_t::reverse_iterator>
    revRange(Container_t container)
    {
    return { container.rbegin(), container.rend() };
    }


    #include <vector>
    #include <iostream>

    int main()
    {
    std::vector<int> v = { 1, 3, 5, 7, 9 };

    for(int i: revRange(v)) std::cout << " " << i;
    std::cout << "\n";
    }
    //----------------------------------------------------------------------

    For some reason it does not work properly when compiled with gcc 4.6.3.
    It prints the first numbers ok, but then it starts printing zeros.

    If I compile with -D_GLIBCXX_DEBUG I get a strange runtime error:

    /usr/include/c++/4.6/debug/safe_iterator.h:142:error: attempt to copy-
    construct an iterator from a singular iterator.

    I don't understand what that means.

    When run with gdb, the backtrace indicates that the error seems to
    happen in RangeWrapper::begin().
     
    Juha Nieminen, May 18, 2012
    #1
    1. Advertising

  2. Juha Nieminen <> wrote:
    > revRange(Container_t container)


    Ah, the bug was here. My bad.
     
    Juha Nieminen, May 18, 2012
    #2
    1. Advertising

  3. Juha Nieminen

    jacob navia Guest

    Le 18/05/12 20:15, Juha Nieminen a écrit :
    > Juha Nieminen<> wrote:
    >> revRange(Container_t container)

    >
    > Ah, the bug was here. My bad.

    ????
    Can you explain what was the bug, why it was there and how you fixed it?
     
    jacob navia, May 19, 2012
    #3
  4. On May 19, 5:56 pm, jacob navia <> wrote:
    > Le 18/05/12 20:15, Juha Nieminen a écrit :> Juha Nieminen<nos...@thanks..invalid>  wrote:
    > >> revRange(Container_t container)

    >
    > >    Ah, the bug was here. My bad.

    >
    > ????
    > Can you explain what was the bug, why it was there and how you fixed it?


    It should have been -

    revRange(Container_t & container)

    ^^^

    Since a the container was passed by value, the iterators that were
    returned were that of the generated copy, not the intended container.
     
    Anand Hariharan, May 20, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo
    Replies:
    0
    Views:
    577
    S. Justin Gengo
    Jul 16, 2003
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  3. Harvey Twyman
    Replies:
    8
    Views:
    586
    August Derleth
    Oct 25, 2003
  4. Michael Coley

    Strange error with iterators

    Michael Coley, Aug 23, 2007, in forum: C++
    Replies:
    6
    Views:
    305
    Jerry Coffin
    Aug 26, 2007
  5. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
Loading...

Share This Page