From 9649e5b62e77adda818b83a9b2fe8fe3fc472d0f Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 17 Jun 2015 12:33:31 +0100 Subject: [PATCH] forward_list.h (forward_list::_M_get_Node_allocator): Remove unnecessary uses of operator& and static_cast. * include/bits/forward_list.h (forward_list::_M_get_Node_allocator): Remove unnecessary uses of operator& and static_cast. * include/bits/forward_list.tcc (forward_list::operator=(const forward_list&)): Use __addressof instead of operator&. (forward_list::remove(const _Tp&), forward_list::remove(_Pred)): Remove invalid static_casts. * include/debug/forward_list: Use __addressof instead of operator&. * testsuite/23_containers/forward_list/modifiers/addressof.cc: New. From-SVN: r224553 --- libstdc++-v3/ChangeLog | 10 ++++++ libstdc++-v3/include/bits/forward_list.h | 4 +-- libstdc++-v3/include/bits/forward_list.tcc | 12 +++---- libstdc++-v3/include/debug/forward_list | 15 +++++---- .../forward_list/modifiers/addressof.cc | 32 +++++++++++++++++++ 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index caa32f7383a..250728c85eb 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,15 @@ 2015-06-17 Jonathan Wakely + * include/bits/forward_list.h (forward_list::_M_get_Node_allocator): + Remove unnecessary uses of operator& and static_cast. + * include/bits/forward_list.tcc + (forward_list::operator=(const forward_list&)): Use __addressof + instead of operator&. + (forward_list::remove(const _Tp&), forward_list::remove(_Pred)): + Remove invalid static_casts. + * include/debug/forward_list: Use __addressof instead of operator&. + * testsuite/23_containers/forward_list/modifiers/addressof.cc: New. + * include/ext/alloc_traits.h (__alloc_traits::_S_nothrow_swap()): Use __is_nothrow_swappable. diff --git a/libstdc++-v3/include/bits/forward_list.h b/libstdc++-v3/include/bits/forward_list.h index dcb696fb72a..d611aded80e 100644 --- a/libstdc++-v3/include/bits/forward_list.h +++ b/libstdc++-v3/include/bits/forward_list.h @@ -305,11 +305,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Node_alloc_type& _M_get_Node_allocator() noexcept - { return *static_cast<_Node_alloc_type*>(&this->_M_impl); } + { return this->_M_impl; } const _Node_alloc_type& _M_get_Node_allocator() const noexcept - { return *static_cast(&this->_M_impl); } + { return this->_M_impl; } _Fwd_list_base() : _M_impl() { } diff --git a/libstdc++-v3/include/bits/forward_list.tcc b/libstdc++-v3/include/bits/forward_list.tcc index 8c85cdcadcf..1e1a02e1d02 100644 --- a/libstdc++-v3/include/bits/forward_list.tcc +++ b/libstdc++-v3/include/bits/forward_list.tcc @@ -155,7 +155,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER forward_list<_Tp, _Alloc>:: operator=(const forward_list& __list) { - if (&__list != this) + if (std::__addressof(__list) != this) { if (_Node_alloc_traits::_S_propagate_on_copy_assign()) { @@ -299,8 +299,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER forward_list<_Tp, _Alloc>:: remove(const _Tp& __val) { - _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head); - _Node* __extra = 0; + _Node_base* __curr = &this->_M_impl._M_head; + _Node_base* __extra = nullptr; while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next)) { @@ -314,7 +314,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER else __extra = __curr; } - __curr = static_cast<_Node*>(__curr->_M_next); + __curr = __curr->_M_next; } if (__extra) @@ -327,13 +327,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER forward_list<_Tp, _Alloc>:: remove_if(_Pred __pred) { - _Node* __curr = static_cast<_Node*>(&this->_M_impl._M_head); + _Node_base* __curr = &this->_M_impl._M_head; while (_Node* __tmp = static_cast<_Node*>(__curr->_M_next)) { if (__pred(*__tmp->_M_valptr())) this->_M_erase_after(__curr); else - __curr = static_cast<_Node*>(__curr->_M_next); + __curr = __curr->_M_next; } } diff --git a/libstdc++-v3/include/debug/forward_list b/libstdc++-v3/include/debug/forward_list index 0f09d23e2fd..d22c22fae41 100644 --- a/libstdc++-v3/include/debug/forward_list +++ b/libstdc++-v3/include/debug/forward_list @@ -105,7 +105,7 @@ namespace __gnu_debug __bbegin_its = __victim_base; } else - __victim_base->_M_sequence = &__lhs; + __victim_base->_M_sequence = std::__addressof(__lhs); } if (__bbegin_its) @@ -523,7 +523,7 @@ namespace __debug splice_after(const_iterator __pos, forward_list&& __list) { __glibcxx_check_insert_after(__pos); - _GLIBCXX_DEBUG_VERIFY(&__list != this, + _GLIBCXX_DEBUG_VERIFY(std::__addressof(__list) != this, _M_message(__gnu_debug::__msg_self_splice) ._M_sequence(*this, "this")); _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(), @@ -550,7 +550,7 @@ namespace __debug _GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(), _M_message(__gnu_debug::__msg_splice_bad) ._M_iterator(__i, "__i")); - _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(&__list), + _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__list)), _M_message(__gnu_debug::__msg_splice_other) ._M_iterator(__i, "__i") ._M_sequence(__list, "__list")); @@ -577,9 +577,10 @@ namespace __debug splice_after(const_iterator __pos, forward_list&& __list, const_iterator __before, const_iterator __last) { + auto __listptr = std::__addressof(__list); __glibcxx_check_insert_after(__pos); __glibcxx_check_valid_range(__before, __last); - _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(&__list), + _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr), _M_message(__gnu_debug::__msg_splice_other) ._M_sequence(__list, "list") ._M_iterator(__before, "before")); @@ -607,7 +608,7 @@ namespace __debug ._M_sequence(__list, "list") ._M_iterator(__before, "before") ._M_iterator(__last, "last")); - _GLIBCXX_DEBUG_VERIFY(&__list != this || __tmp != __pos.base(), + _GLIBCXX_DEBUG_VERIFY(__listptr != this || __tmp != __pos.base(), _M_message(__gnu_debug::__msg_splice_overlap) ._M_iterator(__tmp, "position") ._M_iterator(__before, "before") @@ -694,7 +695,7 @@ namespace __debug void merge(forward_list&& __list) { - if (this != &__list) + if (this != std::__addressof(__list)) { __glibcxx_check_sorted(_Base::begin(), _Base::end()); __glibcxx_check_sorted(__list._M_base().begin(), @@ -716,7 +717,7 @@ namespace __debug void merge(forward_list&& __list, _Comp __comp) { - if (this != &__list) + if (this != std::__addressof(__list)) { __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp); __glibcxx_check_sorted_pred(__list._M_base().begin(), diff --git a/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc new file mode 100644 index 00000000000..3c325f7b9ba --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/forward_list/modifiers/addressof.cc @@ -0,0 +1,32 @@ +// Copyright (C) 2015 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++11" } +// { dg-do compile } + +#include + +namespace N +{ + // This operator& must not be found by ADL. + template void operator&(const T&) { } + struct X { }; + bool operator==(const X&, const X&); + bool operator<(const X&, const X&); +} + +template class std::forward_list; -- 2.30.2