Make std::list::iterator == and != global inline friend

Message ID b5312ca4-bbbd-8b47-22e6-168538819afc@gmail.com
State New
Headers show
Series
  • Make std::list::iterator == and != global inline friend
Related show

Commit Message

François Dumont Oct. 9, 2018, 5:25 a.m.
As we talked one day I would like to make all iterator operators global 
for consistency. So here is the patch to do so for std::list iterators.

Thanks to this change the operators ==(iterator, const_iterator) and != 
are not necessary anymore, one less ==|!= operator candidate.

     * include/bits/stl_list.h
     (_List_operator<>::operator==): Make global inline friend.
     (_List_operator<>::operator!=): Likewise.
     (_List_const_operator<>::operator==): Likewise.
     (_List_const_operator<>::operator!=): Likewise.
     (operator==(const _List_iterator<>&, const _List_const_iterator<>&)):
     Remove.
     (operator!=(const _List_iterator<>&, const _List_const_iterator<>&)):
     Remove.

Tested under Linux x86_64.

Ok to commit ?

François

Comments

Jonathan Wakely Oct. 9, 2018, 11:31 a.m. | #1
On 09/10/18 07:25 +0200, François Dumont wrote:
>As we talked one day I would like to make all iterator operators 
>global for consistency. So here is the patch to do so for std::list 
>iterators.

By "global" you mean "non-member", right?

>
>Thanks to this change the operators ==(iterator, const_iterator) and 
>!= are not necessary anymore, one less ==|!= operator candidate.
>
>    * include/bits/stl_list.h
>    (_List_operator<>::operator==): Make global inline friend.

I find this description a bit confusing. Please use "Replace member
function with inline friend" instead.

>    (_List_operator<>::operator!=): Likewise.
>    (_List_const_operator<>::operator==): Likewise.
>    (_List_const_operator<>::operator!=): Likewise.
>    (operator==(const _List_iterator<>&, const _List_const_iterator<>&)):
>    Remove.
>    (operator!=(const _List_iterator<>&, const _List_const_iterator<>&)):
>    Remove.
>
>Tested under Linux x86_64.
>
>Ok to commit ?

OK with the revised changelog text, thanks.
François Dumont Oct. 10, 2018, 5:20 a.m. | #2
On 10/09/2018 01:31 PM, Jonathan Wakely wrote:
> On 09/10/18 07:25 +0200, François Dumont wrote:
>> As we talked one day I would like to make all iterator operators 
>> global for consistency. So here is the patch to do so for std::list 
>> iterators.
>
> By "global" you mean "non-member", right?

Yes, committed with your advised change.

Patch

diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h
index 47749142e0e..3544981698c 100644
--- a/libstdc++-v3/include/bits/stl_list.h
+++ b/libstdc++-v3/include/bits/stl_list.h
@@ -243,13 +243,13 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	return __tmp;
       }
 
-      bool
-      operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
-      { return _M_node == __x._M_node; }
+      friend bool
+      operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+      { return __x._M_node == __y._M_node; }
 
-      bool
-      operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
-      { return _M_node != __x._M_node; }
+      friend bool
+      operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+      { return __x._M_node != __y._M_node; }
 
       // The only member points to the %list element.
       __detail::_List_node_base* _M_node;
@@ -327,30 +327,18 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	return __tmp;
       }
 
-      bool
-      operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
-      { return _M_node == __x._M_node; }
+      friend bool
+      operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+      { return __x._M_node == __y._M_node; }
 
-      bool
-      operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
-      { return _M_node != __x._M_node; }
+      friend bool
+      operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
+      { return __x._M_node != __y._M_node; }
 
       // The only member points to the %list element.
       const __detail::_List_node_base* _M_node;
     };
 
-  template<typename _Val>
-    inline bool
-    operator==(const _List_iterator<_Val>& __x,
-	       const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
-    { return __x._M_node == __y._M_node; }
-
-  template<typename _Val>
-    inline bool
-    operator!=(const _List_iterator<_Val>& __x,
-	       const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
-    { return __x._M_node != __y._M_node; }
-
 _GLIBCXX_BEGIN_NAMESPACE_CXX11
   /// See bits/stl_deque.h's _Deque_base for an explanation.
   template<typename _Tp, typename _Alloc>