NULL iterators for <list>

Howard Hinnant hhinnant at apple.com
Fri Aug 2 11:24:26 PDT 2013


On Aug 1, 2013, at 1:05 PM, Marshall Clow <mclow.lists at gmail.com> wrote:

> http://std.org/jtc1/sc22/wg21/docs/papers/2013/n3644.pdf  says that value-initialized iterators should compare equal.
> 
> Implement that for std::list.
> 
> There are interactions with libc++'s debug mode; hence the extra tests.

Looks pretty good.  Just a couple of comments.

1.

     friend _LIBCPP_INLINE_VISIBILITY
     bool operator==(const __list_iterator& __x, const __list_iterator& __y)
     {
+    if ( __x.__ptr_ == nullptr )
+    	return  __y.__ptr_ == nullptr;

This is unnecessary, and hurts performance.  The existing:

        return __x.__ptr_ == __y.__ptr_;

is all we need.

2.

In both tests you have some iterators default constructed:

+    C::iterator i;

This isn't sufficient to get the desired behavior, unless you take advantage of knowledge of the current implementation.  For example this test would sometimes fail if the implementation changed from:

+    __list_iterator() _NOEXCEPT : __ptr_(nullptr)

to:

+    __list_iterator() = default;

In order to leave open the possibility of changing the implementation without having to re-implement the tests, the tests should always do one of:

    C::iterator i{};
    C::iterator i = C::iterator{};
    C::iterator i = C::iterator();

Thanks!

Howard




More information about the cfe-commits mailing list