1

I am facing a problem with splicing the list with itself. Note that I have gone through splice() on std::list and iterator invalidation There the question was about two different lists. But my question is about the same list.

mylist.splice(mylist.end(), mylist, ++mylist.begin());

It seems that gcc 3.x is invalidating the moved iterator. So I suppose it is deallocating and allocating the node again. This does not make sense for the same list. SGI does tell that this version of splice should not invalidate any iterators. Is this a bug with gcc 3.x, if it is there any workaround?

In the mean time I was going through the stl_list.h file. But stuck with the transfer() function, I could not find a definition for these.

struct _List_node_base
  {
    _List_node_base* _M_next;   ///< Self-explanatory
    _List_node_base* _M_prev;   ///< Self-explanatory

    static void
    swap(_List_node_base& __x, _List_node_base& __y);

    void
    transfer(_List_node_base * const __first,
         _List_node_base * const __last);

    void
    reverse();

    void
    hook(_List_node_base * const __position);

    void
    unhook();
  };

Do you have any idea where can I look for these function definitions?

Community
  • 1
  • 1
ashim
  • 231
  • 1
  • 2
  • 5

1 Answers1

0

This functions are in the libstdc++ sources, not the headers. In 3.4 it's in libstdc++-v3/src/list.cc

http://gcc.gnu.org/viewcvs/branches/gcc-3_4-branch/libstdc%2B%2B-v3/src/list.cc?view=markup

Have you tried compiling with -D_GLIBCXX_DEBUG ? That will enable the Debug Mode and tell you if you're using invalid iterators or anything else that causes the problem.

I just tried this simple test with GCC 3.4, with and without debug mode, and it worked fine:

#include <list>
#include <iostream>
#include <string>

int main()
{
  std::list<std::string> l;
  l.push_back("1");
  l.push_back("2");
  l.push_back("3");
  l.push_back("4");
  l.push_back("5");
  l.push_back("6");
  l.splice(l.end(), l, ++l.begin());

  for (std::list<std::string>::iterator i = l.begin(), e = l.end(); i != e; ++i)
    std::cout << *i << ' ';
  std::cout << std::endl;
}

Modifying it further and debugging it I see that no element is destroyed and reallocated when doing the splice, so I suspect the bug is in your program. It's hard to know, as you haven't actually said what the problem is.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521