No NEXT in <forward_list>?????

Why is a quick easy <NEXT> member function not explicitly part of the forward_list implementation. Also, no c++ reference sites or the isoc++ page enumerate a quick easy way to get the next element in the singly linked list (SLL). The reason for a SLL is to be fast and a double deref *(itor->_M_next) is very fast compared to the alternatives. Obviously, another iterator can be kept and paced to one's algorithm, but this is another operation that must occur every iteration. Alternatively, the _M_next variable can be teased out of the header, but why the extra research? When I contemplate the massive number of SLLs I have written, they have always included a double deref, so that is enough for me to consider this an important function. So why isn't it explicitly dealt with?

Or am I missing something obvious outside of the alternatives I listed above.

I am looking to participate in the C++ standard, so I am trying to get a feel for the committee's reasoning. At this point, I am going to assume that anything which can be accomplished with the given public members should be considered axiomatic?

Or maybe this can be considered a document bug?

Or maybe this is a social tagging ^?

If you see weird things like identifiers with {underscore}{capital letter}, usually that means they are reserved for internal use by the library/compiler.

If you have an iterator for a forward list, you can use std::next for the next element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <forward_list>
#include <iterator>

int main()
{
    std::forward_list<int> list;
    list.push_front(42);
    list.push_front(1729);
    
    auto it = list.begin();
    std::cout << (*it) << '\n';
    std::cout << *std::next(it, 1) << '\n';
}

(Could also use std::advance, if not C++11)
Last edited on
Thanks, bulls-eye answer.

I was hoping to find a class member function to the forward_list or to the iterator, as they are generally more efficient than the stand alone functions. (One of Scott Meyer's More Effective C++ tips.) Unfortunately, none exist, apparently.

I was hoping to find a class member function to the forward_list or to the iterator, as they are generally more efficient than the stand alone functions. (One of Scott Meyer's More Effective C++ tips.)
Where's that in the book? :)
Topic archived. No new replies allowed.