Implement full support for non-pointer pointers in custom allocators for list.

llvm-svn: 184859
diff --git a/libcxx/include/list b/libcxx/include/list
index 8f80848..628a35b 100644
--- a/libcxx/include/list
+++ b/libcxx/include/list
@@ -196,13 +196,20 @@
         rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
 #endif
 
+    typedef typename pointer_traits<_VoidPtr>::template
+#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
+        rebind<__list_node_base> __base_pointer;
+#else
+        rebind<__list_node_base>::other __base_pointer;
+#endif
+
     pointer __prev_;
     pointer __next_;
 
     _LIBCPP_INLINE_VISIBILITY
     __list_node_base()
-        : __prev_(static_cast<pointer>(this)),
-          __next_(static_cast<pointer>(this))
+        : __prev_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this))),
+          __next_(static_cast<pointer>(pointer_traits<__base_pointer>::pointer_to(*this)))
           {}
 };
 
@@ -305,7 +312,14 @@
         return __ptr_->__value_;
     }
     _LIBCPP_INLINE_VISIBILITY
-    pointer operator->() const {return &(operator*());}
+    pointer operator->() const
+    {
+#if _LIBCPP_DEBUG_LEVEL >= 2
+        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
+                       "Attempted to dereference a non-dereferenceable list::iterator");
+#endif
+        return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
+    }
 
     _LIBCPP_INLINE_VISIBILITY
     __list_iterator& operator++()
@@ -352,9 +366,9 @@
 {
     typedef typename pointer_traits<_VoidPtr>::template
 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
-        rebind<const __list_node<_Tp, _VoidPtr> > __node_pointer;
+        rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
 #else
-        rebind<const __list_node<_Tp, _VoidPtr> >::other __node_pointer;
+        rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
 #endif
 
     __node_pointer __ptr_;
@@ -439,7 +453,14 @@
         return __ptr_->__value_;
     }
     _LIBCPP_INLINE_VISIBILITY
-    pointer operator->() const {return &(operator*());}
+    pointer operator->() const
+    {
+#if _LIBCPP_DEBUG_LEVEL >= 2
+        _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this),
+                       "Attempted to dereference a non-dereferenceable list::iterator");
+#endif
+        return pointer_traits<pointer>::pointer_to(__ptr_->__value_);
+    }
 
     _LIBCPP_INLINE_VISIBILITY
     __list_const_iterator& operator++()
@@ -505,11 +526,20 @@
                                                                      __node_allocator;
     typedef allocator_traits<__node_allocator>                       __node_alloc_traits;
     typedef typename __node_alloc_traits::pointer                    __node_pointer;
-    typedef typename __node_alloc_traits::const_pointer              __node_const_pointer;
+    typedef typename __node_alloc_traits::pointer                    __node_const_pointer;
     typedef typename __alloc_traits::pointer                         pointer;
     typedef typename __alloc_traits::const_pointer                   const_pointer;
     typedef typename __alloc_traits::difference_type                 difference_type;
 
+    typedef typename __alloc_traits::template
+#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
+                rebind_alloc<__node_base>
+#else
+                rebind_alloc<__node_base>::other
+#endif
+                                                                     __node_base_allocator;
+    typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer;
+
     __node_base __end_;
     __compressed_pair<size_type, __node_allocator> __size_alloc_;
 
@@ -525,7 +555,7 @@
     const __node_allocator& __node_alloc() const _NOEXCEPT
         {return __size_alloc_.second();}
 
-    static void __unlink_nodes(__node_base& __f, __node_base& __l) _NOEXCEPT;
+    static void __unlink_nodes(__node_pointer __f, __node_pointer __l) _NOEXCEPT;
 
     __list_imp()
         _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
@@ -557,18 +587,22 @@
     iterator end() _NOEXCEPT
     {
 #if _LIBCPP_DEBUG_LEVEL >= 2
-        return iterator(static_cast<__node_pointer>(&__end_), this);
+        return iterator(static_cast<__node_pointer>(
+                pointer_traits<__node_base_pointer>::pointer_to(__end_)), this);
 #else
-        return iterator(static_cast<__node_pointer>(&__end_));
+        return iterator(static_cast<__node_pointer>(
+                      pointer_traits<__node_base_pointer>::pointer_to(__end_)));
 #endif
     }
     _LIBCPP_INLINE_VISIBILITY
     const_iterator end() const _NOEXCEPT
     {
 #if _LIBCPP_DEBUG_LEVEL >= 2
-        return const_iterator(static_cast<__node_const_pointer>(&__end_), this);
+        return const_iterator(static_cast<__node_const_pointer>(
+        pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))), this);
 #else
-        return const_iterator(static_cast<__node_const_pointer>(&__end_));
+        return const_iterator(static_cast<__node_const_pointer>(
+        pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(__end_))));
 #endif
     }
 
@@ -637,11 +671,11 @@
 template <class _Tp, class _Alloc>
 inline _LIBCPP_INLINE_VISIBILITY
 void
-__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_base& __f, __node_base& __l)
+__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_pointer __f, __node_pointer __l)
     _NOEXCEPT
 {
-    __f.__prev_->__next_ = __l.__next_;
-    __l.__next_->__prev_ = __f.__prev_;
+    __f->__prev_->__next_ = __l->__next_;
+    __l->__next_->__prev_ = __f->__prev_;
 }
 
 template <class _Tp, class _Alloc>
@@ -676,15 +710,16 @@
     {
         __node_allocator& __na = __node_alloc();
         __node_pointer __f = __end_.__next_;
-        __node_pointer __l = static_cast<__node_pointer>(&__end_);
-        __unlink_nodes(*__f, *__l->__prev_);
+        __node_pointer __l = static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__end_));
+        __unlink_nodes(__f, __l->__prev_);
         __sz() = 0;
         while (__f != __l)
         {
-            __node& __n = *__f;
+            __node_pointer __n = __f;
             __f = __f->__next_;
-            __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
-            __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
+            __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
+            __node_alloc_traits::deallocate(__na, __n, 1);
         }
 #if _LIBCPP_DEBUG_LEVEL >= 2
         __c_node* __c = __get_db()->__find_c_and_lock(this);
@@ -719,16 +754,20 @@
     swap(__sz(), __c.__sz());
     swap(__end_, __c.__end_);
     if (__sz() == 0)
-        __end_.__next_ = __end_.__prev_ = &static_cast<__node&>(__end_);
+        __end_.__next_ = __end_.__prev_ = static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__end_));
     else
         __end_.__prev_->__next_ = __end_.__next_->__prev_
-                                = &static_cast<__node&>(__end_);
+                                = static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__end_));
     if (__c.__sz() == 0)
         __c.__end_.__next_ = __c.__end_.__prev_
-                           = &static_cast<__node&>(__c.__end_);
+                           = static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
     else
         __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
-                                    = &static_cast<__node&>(__c.__end_);
+                                    = static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__c.__end_));
 #if _LIBCPP_DEBUG_LEVEL >= 2
     __libcpp_db* __db = __get_db();
     __c_node* __cn1 = __db->__find_c_and_lock(this);
@@ -740,7 +779,8 @@
     {
         --__p;
         const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
-        if (__i->__ptr_ == static_cast<__node_pointer>(&__c.__end_))
+        if (__i->__ptr_ == static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
         {
             __cn2->__add(*__p);
             if (--__cn1->end_ != __p)
@@ -753,7 +793,8 @@
     {
         --__p;
         const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
-        if (__i->__ptr_ == static_cast<__node_pointer>(&__end_))
+        if (__i->__ptr_ == static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__end_)))
         {
             __cn1->__add(*__p);
             if (--__cn2->end_ != __p)
@@ -775,6 +816,8 @@
     typedef typename base::__node_allocator    __node_allocator;
     typedef typename base::__node_pointer      __node_pointer;
     typedef typename base::__node_alloc_traits __node_alloc_traits;
+    typedef typename base::__node_base         __node_base;
+    typedef typename base::__node_base_pointer __node_base_pointer;
 
 public:
     typedef _Tp                                      value_type;
@@ -1014,7 +1057,7 @@
 #endif  // _LIBCPP_DEBUG_LEVEL >= 2
 
 private:
-    static void __link_nodes(__node& __p, __node& __f, __node& __l);
+    static void __link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l);
     iterator __iterator(size_type __n);
     template <class _Comp>
         static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
@@ -1028,12 +1071,12 @@
 template <class _Tp, class _Alloc>
 inline _LIBCPP_INLINE_VISIBILITY
 void
-list<_Tp, _Alloc>::__link_nodes(__node& __p, __node& __f, __node& __l)
+list<_Tp, _Alloc>::__link_nodes(__node_pointer __p, __node_pointer __f, __node_pointer __l)
 {
-    __p.__prev_->__next_ = &__f;
-    __f.__prev_ = __p.__prev_;
-    __p.__prev_ = &__l;
-    __l.__next_ = &__p;
+    __p->__prev_->__next_ = __f;
+    __f->__prev_ = __p->__prev_;
+    __p->__prev_ = __l;
+    __l->__next_ = __p;
 }
 
 template <class _Tp, class _Alloc>
@@ -1290,7 +1333,7 @@
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __hold->__prev_ = 0;
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
-    __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
+    __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
     ++base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
     return iterator(__hold.release(), this);
@@ -1307,9 +1350,9 @@
     _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
         "list::insert(iterator, n, x) called with an iterator not"
         " referring to this list");
-    iterator __r(const_cast<__node_pointer>(__p.__ptr_), this);
+    iterator __r(__p.__ptr_, this);
 #else
-    iterator __r(const_cast<__node_pointer>(__p.__ptr_));
+    iterator __r(__p.__ptr_);
 #endif
     if (__n > 0)
     {
@@ -1359,7 +1402,7 @@
             throw;
         }
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
+        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
         base::__sz() += __ds;
     }
     return __r;
@@ -1375,9 +1418,9 @@
     _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
         "list::insert(iterator, range) called with an iterator not"
         " referring to this list");
-    iterator __r(const_cast<__node_pointer>(__p.__ptr_), this);
+    iterator __r(__p.__ptr_, this);
 #else
-    iterator __r(const_cast<__node_pointer>(__p.__ptr_));
+    iterator __r(__p.__ptr_);
 #endif
     if (__f != __l)
     {
@@ -1427,7 +1470,7 @@
             throw;
         }
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
+        __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_);
         base::__sz() += __ds;
     }
     return __r;
@@ -1441,7 +1484,7 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
-    __link_nodes(*base::__end_.__next_, *__hold, *__hold);
+    __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1454,7 +1497,8 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
-    __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
+    __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
+                         pointer_to(base::__end_)), __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1469,7 +1513,7 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
-    __link_nodes(*base::__end_.__next_, *__hold, *__hold);
+    __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1482,7 +1526,8 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
-    __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
+    __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
+                         pointer_to(base::__end_)), __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1498,7 +1543,7 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
-    __link_nodes(*base::__end_.__next_, *__hold, *__hold);
+    __link_nodes(base::__end_.__next_, __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1512,7 +1557,8 @@
     typedef __allocator_destructor<__node_allocator> _Dp;
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
-    __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
+    __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
+                         pointer_to(base::__end_)), __hold.get(), __hold.get());
     ++base::__sz();
     __hold.release();
 }
@@ -1532,7 +1578,7 @@
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __hold->__prev_ = 0;
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
-    __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
+    __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
     ++base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
     return iterator(__hold.release(), this);
@@ -1557,7 +1603,7 @@
     unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1));
     __hold->__prev_ = 0;
     __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
-    __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
+    __link_nodes(__p.__ptr_, __hold.get(), __hold.get());
     ++base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
     return iterator(__hold.release(), this);
@@ -1574,7 +1620,7 @@
 {
     _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list");
     __node_allocator& __na = base::__node_alloc();
-    __node& __n = *base::__end_.__next_;
+    __node_pointer __n = base::__end_.__next_;
     base::__unlink_nodes(__n, __n);
     --base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1583,7 +1629,7 @@
     {
         --__p;
         iterator* __i = static_cast<iterator*>((*__p)->__i_);
-        if (__i->__ptr_ == &__n)
+        if (__i->__ptr_ == __n)
         {
             (*__p)->__c_ = nullptr;
             if (--__c->end_ != __p)
@@ -1592,8 +1638,8 @@
     }
     __get_db()->unlock();
 #endif
-    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
-    __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
+    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
+    __node_alloc_traits::deallocate(__na, __n, 1);
 }
 
 template <class _Tp, class _Alloc>
@@ -1602,7 +1648,7 @@
 {
     _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list");
     __node_allocator& __na = base::__node_alloc();
-    __node& __n = *base::__end_.__prev_;
+    __node_pointer __n = base::__end_.__prev_;
     base::__unlink_nodes(__n, __n);
     --base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1611,7 +1657,7 @@
     {
         --__p;
         iterator* __i = static_cast<iterator*>((*__p)->__i_);
-        if (__i->__ptr_ == &__n)
+        if (__i->__ptr_ == __n)
         {
             (*__p)->__c_ = nullptr;
             if (--__c->end_ != __p)
@@ -1620,8 +1666,8 @@
     }
     __get_db()->unlock();
 #endif
-    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
-    __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
+    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
+    __node_alloc_traits::deallocate(__na, __n, 1);
 }
 
 template <class _Tp, class _Alloc>
@@ -1636,8 +1682,8 @@
     _LIBCPP_ASSERT(__p != end(),
         "list::erase(iterator) called with a non-dereferenceable iterator");
     __node_allocator& __na = base::__node_alloc();
-    __node& __n = const_cast<__node&>(*__p.__ptr_);
-    __node_pointer __r = __n.__next_;
+    __node_pointer __n = __p.__ptr_;
+    __node_pointer __r = __n->__next_;
     base::__unlink_nodes(__n, __n);
     --base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1646,7 +1692,7 @@
     {
         --__p;
         iterator* __i = static_cast<iterator*>((*__p)->__i_);
-        if (__i->__ptr_ == &__n)
+        if (__i->__ptr_ == __n)
         {
             (*__p)->__c_ = nullptr;
             if (--__c->end_ != __p)
@@ -1655,8 +1701,8 @@
     }
     __get_db()->unlock();
 #endif
-    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
-    __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
+    __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
+    __node_alloc_traits::deallocate(__na, __n, 1);
 #if _LIBCPP_DEBUG_LEVEL >= 2
     return iterator(__r, this);
 #else
@@ -1676,10 +1722,10 @@
     if (__f != __l)
     {
         __node_allocator& __na = base::__node_alloc();
-        base::__unlink_nodes(const_cast<__node&>(*__f.__ptr_), *__l.__ptr_->__prev_);
+        base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_);
         while (__f != __l)
         {
-            __node& __n = const_cast<__node&>(*__f.__ptr_);
+            __node_pointer __n = __f.__ptr_;
             ++__f;
             --base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1688,7 +1734,7 @@
             {
                 --__p;
                 iterator* __i = static_cast<iterator*>((*__p)->__i_);
-                if (__i->__ptr_ == &__n)
+                if (__i->__ptr_ == __n)
                 {
                     (*__p)->__c_ = nullptr;
                     if (--__c->end_ != __p)
@@ -1697,14 +1743,14 @@
             }
             __get_db()->unlock();
 #endif
-            __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
-            __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
+            __node_alloc_traits::destroy(__na, _VSTD::addressof(__n->__value_));
+            __node_alloc_traits::deallocate(__na, __n, 1);
         }
     }
 #if _LIBCPP_DEBUG_LEVEL >= 2
-    return iterator(const_cast<__node_pointer>(__l.__ptr_), this);
+    return iterator(__l.__ptr_, this);
 #else
-    return iterator(const_cast<__node_pointer>(__l.__ptr_));
+    return iterator(__l.__ptr_);
 #endif
 }
 
@@ -1762,7 +1808,8 @@
             throw;
         }
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
+        __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
+                         pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
         base::__sz() += __ds;
     }
 }
@@ -1821,7 +1868,8 @@
             throw;
         }
 #endif  // _LIBCPP_NO_EXCEPTIONS
-        __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
+        __link_nodes(static_cast<__node_pointer>(pointer_traits<__node_base_pointer>::
+                         pointer_to(base::__end_)), __r.__ptr_, __e.__ptr_);
         base::__sz() += __ds;
     }
 }
@@ -1839,10 +1887,10 @@
 #endif
     if (!__c.empty())
     {
-        __node& __f = *__c.__end_.__next_;
-        __node& __l = *__c.__end_.__prev_;
+        __node_pointer __f = __c.__end_.__next_;
+        __node_pointer __l = __c.__end_.__prev_;
         base::__unlink_nodes(__f, __l);
-        __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __l);
+        __link_nodes(__p.__ptr_, __f, __l);
         base::__sz() += __c.__sz();
         __c.__sz() = 0;
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1853,7 +1901,8 @@
         {
             --__p;
             iterator* __i = static_cast<iterator*>((*__p)->__i_);
-            if (__i->__ptr_ != static_cast<__node_pointer>(&__c.__end_))
+            if (__i->__ptr_ != static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
             {
                 __cn1->__add(*__p);
                 (*__p)->__c_ = __cn1;
@@ -1883,9 +1932,9 @@
 #endif
     if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_)
     {
-        __node& __f = const_cast<__node&>(*__i.__ptr_);
+        __node_pointer __f = __i.__ptr_;
         base::__unlink_nodes(__f, __f);
-        __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __f);
+        __link_nodes(__p.__ptr_, __f, __f);
         --__c.__sz();
         ++base::__sz();
 #if _LIBCPP_DEBUG_LEVEL >= 2
@@ -1896,7 +1945,7 @@
         {
             --__p;
             iterator* __j = static_cast<iterator*>((*__p)->__i_);
-            if (__j->__ptr_ == &__f)
+            if (__j->__ptr_ == __f)
             {
                 __cn1->__add(*__p);
                 (*__p)->__c_ = __cn1;
@@ -1937,11 +1986,11 @@
             __c.__sz() -= __s;
             base::__sz() += __s;
         }
-        __node& __first = const_cast<__node&>(*__f.__ptr_);
+        __node_pointer __first = __f.__ptr_;
         --__l;
-        __node& __last = const_cast<__node&>(*__l.__ptr_);
+        __node_pointer __last = __l.__ptr_;
         base::__unlink_nodes(__first, __last);
-        __link_nodes(const_cast<__node&>(*__p.__ptr_), __first, __last);
+        __link_nodes(__p.__ptr_, __first, __last);
 #if _LIBCPP_DEBUG_LEVEL >= 2
         __libcpp_db* __db = __get_db();
         __c_node* __cn1 = __db->__find_c_and_lock(this);
@@ -1950,7 +1999,7 @@
         {
             --__p;
             iterator* __j = static_cast<iterator*>((*__p)->__i_);
-            for (__node_pointer __k = const_cast<__node_pointer>(__f.__ptr_);
+            for (__node_pointer __k = __f.__ptr_;
                                           __k != __l.__ptr_; __k = __k->__next_)
             {
                 if (__j->__ptr_ == __k)
@@ -2056,12 +2105,12 @@
                     ;
                 base::__sz() += __ds;
                 __c.__sz() -= __ds;
-                __node& __f = *__f2.__ptr_;
-                __node& __l = *__m2.__ptr_->__prev_;
+                __node_pointer __f = __f2.__ptr_;
+                __node_pointer __l = __m2.__ptr_->__prev_;
                 __f2 = __m2;
                 base::__unlink_nodes(__f, __l);
                 __m2 = _VSTD::next(__f1);
-                __link_nodes(*__f1.__ptr_, __f, __l);
+                __link_nodes(__f1.__ptr_, __f, __l);
                 __f1 = __m2;
             }
             else
@@ -2076,7 +2125,8 @@
         {
             --__p;
             iterator* __i = static_cast<iterator*>((*__p)->__i_);
-            if (__i->__ptr_ != static_cast<__node_pointer>(&__c.__end_))
+            if (__i->__ptr_ != static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(__c.__end_)))
             {
                 __cn1->__add(*__p);
                 (*__p)->__c_ = __cn1;
@@ -2119,9 +2169,9 @@
     case 2:
         if (__comp(*--__e2, *__f1))
         {
-            __node& __f = *__e2.__ptr_;
+            __node_pointer __f = __e2.__ptr_;
             base::__unlink_nodes(__f, __f);
-            __link_nodes(*__f1.__ptr_, __f, __f);
+            __link_nodes(__f1.__ptr_, __f, __f);
             return __e2;
         }
         return __f1;
@@ -2135,13 +2185,13 @@
         iterator __m2 = _VSTD::next(__f2);
         for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
             ;
-        __node& __f = *__f2.__ptr_;
-        __node& __l = *__m2.__ptr_->__prev_;
+        __node_pointer __f = __f2.__ptr_;
+        __node_pointer __l = __m2.__ptr_->__prev_;
         __r = __f2;
         __e1 = __f2 = __m2;
         base::__unlink_nodes(__f, __l);
         __m2 = _VSTD::next(__f1);
-        __link_nodes(*__f1.__ptr_, __f, __l);
+        __link_nodes(__f1.__ptr_, __f, __l);
         __f1 = __m2;
     }
     else
@@ -2153,14 +2203,14 @@
             iterator __m2 = _VSTD::next(__f2);
             for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
                 ;
-            __node& __f = *__f2.__ptr_;
-            __node& __l = *__m2.__ptr_->__prev_;
+            __node_pointer __f = __f2.__ptr_;
+            __node_pointer __l = __m2.__ptr_->__prev_;
             if (__e1 == __f2)
                 __e1 = __m2;
             __f2 = __m2;
             base::__unlink_nodes(__f, __l);
             __m2 = _VSTD::next(__f1);
-            __link_nodes(*__f1.__ptr_, __f, __l);
+            __link_nodes(__f1.__ptr_, __f, __l);
             __f1 = __m2;
         }
         else
@@ -2198,7 +2248,8 @@
 bool
 list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const
 {
-    return __i->__ptr_ != &this->__end_;
+    return __i->__ptr_ != static_cast<__node_pointer>(
+                       pointer_traits<__node_base_pointer>::pointer_to(const_cast<__node_base&>(this->__end_)));
 }
 
 template <class _Tp, class _Alloc>