libstdc++
forward_list
Go to the documentation of this file.
00001 // <forward_list> -*- C++ -*-
00002 
00003 // Copyright (C) 2010-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file debug/forward_list
00026  *  This file is a GNU debug extension to the Standard C++ Library.
00027  */
00028 
00029 #ifndef _GLIBCXX_DEBUG_FORWARD_LIST
00030 #define _GLIBCXX_DEBUG_FORWARD_LIST 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <bits/c++config.h>
00035 namespace std _GLIBCXX_VISIBILITY(default) { namespace __debug {
00036   template<typename _Tp, typename _Allocator> class forward_list;
00037 } } // namespace std::__debug
00038 
00039 #include <forward_list>
00040 #include <debug/safe_sequence.h>
00041 #include <debug/safe_container.h>
00042 #include <debug/safe_iterator.h>
00043 
00044 // Special validity check for forward_list ranges.
00045 #define __glibcxx_check_valid_fl_range(_First,_Last,_Dist)              \
00046 _GLIBCXX_DEBUG_VERIFY(_First._M_valid_range(_Last, _Dist, false),       \
00047                       _M_message(__gnu_debug::__msg_valid_range)        \
00048                       ._M_iterator(_First, #_First)                     \
00049                       ._M_iterator(_Last, #_Last))
00050 
00051 namespace __gnu_debug
00052 {
00053   /// Special iterators swap and invalidation for forward_list because of the
00054   /// before_begin iterator.
00055   template<typename _SafeSequence>
00056     class _Safe_forward_list
00057     : public _Safe_sequence<_SafeSequence>
00058     {
00059       _SafeSequence&
00060       _M_this() noexcept
00061       { return *static_cast<_SafeSequence*>(this); }
00062 
00063       static void
00064       _M_swap_aux(_Safe_sequence_base& __lhs,
00065                   _Safe_iterator_base*& __lhs_iterators,
00066                   _Safe_sequence_base& __rhs,
00067                   _Safe_iterator_base*& __rhs_iterators);
00068 
00069       void _M_swap_single(_Safe_sequence_base&) noexcept;
00070 
00071     protected:
00072       void
00073       _M_invalidate_all()
00074       {
00075         using _Base_const_iterator = __decltype(_M_this()._M_base().cend());
00076         this->_M_invalidate_if([this](_Base_const_iterator __it)
00077         {
00078           return __it != _M_this()._M_base().cbefore_begin()
00079             && __it != _M_this()._M_base().cend(); });
00080       }
00081 
00082       void _M_swap(_Safe_sequence_base&) noexcept;
00083     };
00084 
00085    template<typename _SafeSequence>
00086     void
00087     _Safe_forward_list<_SafeSequence>::
00088     _M_swap_aux(_Safe_sequence_base& __lhs,
00089                 _Safe_iterator_base*& __lhs_iterators,
00090                 _Safe_sequence_base& __rhs,
00091                 _Safe_iterator_base*& __rhs_iterators)
00092     {
00093       using const_iterator = typename _SafeSequence::const_iterator;
00094       _Safe_iterator_base* __bbegin_its = 0;
00095       _Safe_iterator_base* __last_bbegin = 0;
00096       _SafeSequence& __rseq = static_cast<_SafeSequence&>(__rhs);
00097 
00098       for (_Safe_iterator_base* __iter = __lhs_iterators; __iter;)
00099         {
00100           // Even iterator is cast to const_iterator, not a problem.
00101           _Safe_iterator_base* __victim_base = __iter;
00102           const_iterator* __victim =
00103             static_cast<const_iterator*>(__victim_base);
00104           __iter = __iter->_M_next;
00105           if (__victim->base() == __rseq._M_base().cbefore_begin())
00106             {
00107               __victim->_M_unlink();
00108               if (__lhs_iterators == __victim_base)
00109                 __lhs_iterators = __victim_base->_M_next;
00110               if (__bbegin_its)
00111                 {
00112                   __victim_base->_M_next = __bbegin_its;
00113                   __bbegin_its->_M_prior = __victim_base;
00114                 }
00115               else
00116                 __last_bbegin = __victim_base;
00117               __bbegin_its = __victim_base;
00118             }
00119           else
00120             __victim_base->_M_sequence = std::__addressof(__lhs);
00121         }
00122 
00123       if (__bbegin_its)
00124         {
00125           if (__rhs_iterators)
00126             {
00127               __rhs_iterators->_M_prior = __last_bbegin;
00128               __last_bbegin->_M_next = __rhs_iterators;
00129             }
00130           __rhs_iterators = __bbegin_its;
00131         }
00132     }
00133 
00134    template<typename _SafeSequence>
00135     void
00136     _Safe_forward_list<_SafeSequence>::
00137     _M_swap_single(_Safe_sequence_base& __other) noexcept
00138     {
00139       std::swap(_M_this()._M_iterators, __other._M_iterators);
00140       std::swap(_M_this()._M_const_iterators, __other._M_const_iterators);
00141       // Useless, always 1 on forward_list
00142       //std::swap(_M_this()_M_version, __other._M_version);
00143       _Safe_iterator_base* __this_its = _M_this()._M_iterators;
00144       _M_swap_aux(__other, __other._M_iterators,
00145                   _M_this(), _M_this()._M_iterators);
00146       _Safe_iterator_base* __this_const_its = _M_this()._M_const_iterators;
00147       _M_swap_aux(__other, __other._M_const_iterators,
00148                   _M_this(), _M_this()._M_const_iterators);
00149       _M_swap_aux(_M_this(), __this_its,
00150                   __other, __other._M_iterators);
00151       _M_swap_aux(_M_this(), __this_const_its,
00152                   __other, __other._M_const_iterators);
00153     }
00154 
00155   /* Special forward_list _M_swap version that does not swap the
00156    * before-begin ownership.*/
00157    template<typename _SafeSequence>
00158     void
00159     _Safe_forward_list<_SafeSequence>::
00160     _M_swap(_Safe_sequence_base& __other) noexcept
00161     {
00162       // We need to lock both sequences to swap
00163       using namespace __gnu_cxx;
00164       __mutex *__this_mutex = &_M_this()._M_get_mutex();
00165       __mutex *__other_mutex =
00166         &static_cast<_SafeSequence&>(__other)._M_get_mutex();
00167       if (__this_mutex == __other_mutex)
00168         {
00169           __scoped_lock __lock(*__this_mutex);
00170           _M_swap_single(__other);
00171         }
00172       else
00173         {
00174           __scoped_lock __l1(__this_mutex < __other_mutex
00175                              ? *__this_mutex : *__other_mutex);
00176           __scoped_lock __l2(__this_mutex < __other_mutex
00177                              ? *__other_mutex : *__this_mutex);
00178           _M_swap_single(__other);
00179         }
00180     }
00181 }
00182 
00183 namespace std _GLIBCXX_VISIBILITY(default)
00184 {
00185 namespace __debug
00186 {
00187   /// Class std::forward_list with safety/checking/debug instrumentation.
00188   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
00189     class forward_list
00190     : public __gnu_debug::_Safe_container<
00191         forward_list<_Tp, _Alloc>, _Alloc, __gnu_debug::_Safe_forward_list>,
00192       public _GLIBCXX_STD_C::forward_list<_Tp, _Alloc>
00193     {
00194       typedef _GLIBCXX_STD_C::forward_list<_Tp, _Alloc>         _Base;
00195       typedef __gnu_debug::_Safe_container<
00196         forward_list, _Alloc, __gnu_debug::_Safe_forward_list>  _Safe;
00197 
00198       typedef typename _Base::iterator          _Base_iterator;
00199       typedef typename _Base::const_iterator    _Base_const_iterator;
00200 
00201       template<typename _ItT, typename _SeqT, typename _CatT>
00202         friend class ::__gnu_debug::_Safe_iterator;
00203 
00204     public:
00205       typedef typename _Base::reference         reference;
00206       typedef typename _Base::const_reference   const_reference;
00207 
00208       typedef __gnu_debug::_Safe_iterator<
00209         _Base_iterator, forward_list>           iterator;
00210       typedef __gnu_debug::_Safe_iterator<
00211         _Base_const_iterator, forward_list>     const_iterator;
00212 
00213       typedef typename _Base::size_type         size_type;
00214       typedef typename _Base::difference_type   difference_type;
00215 
00216       typedef _Tp                               value_type;
00217       typedef typename _Base::allocator_type    allocator_type;
00218       typedef typename _Base::pointer           pointer;
00219       typedef typename _Base::const_pointer     const_pointer;
00220 
00221       // 23.2.3.1 construct/copy/destroy:
00222 
00223       forward_list() = default;
00224 
00225       explicit
00226       forward_list(const allocator_type& __al) noexcept
00227       : _Base(__al) { }
00228 
00229       forward_list(const forward_list& __list, const allocator_type& __al)
00230       : _Base(__list, __al)
00231       { }
00232 
00233       forward_list(forward_list&& __list, const allocator_type& __al)
00234         : _Safe(std::move(__list._M_safe()), __al),
00235           _Base(std::move(__list._M_base()), __al)
00236       { }
00237 
00238       explicit
00239       forward_list(size_type __n, const allocator_type& __al = allocator_type())
00240       : _Base(__n, __al)
00241       { }
00242 
00243       forward_list(size_type __n, const _Tp& __value,
00244                    const allocator_type& __al = allocator_type())
00245       : _Base(__n, __value, __al)
00246       { }
00247 
00248       template<typename _InputIterator,
00249                typename = std::_RequireInputIter<_InputIterator>>
00250         forward_list(_InputIterator __first, _InputIterator __last,
00251                      const allocator_type& __al = allocator_type())
00252         : _Base(__gnu_debug::__base(
00253                   __glibcxx_check_valid_constructor_range(__first, __last)),
00254                 __gnu_debug::__base(__last), __al)
00255         { }
00256 
00257       forward_list(const forward_list&) = default;
00258 
00259       forward_list(forward_list&&) = default;
00260 
00261       forward_list(std::initializer_list<_Tp> __il,
00262                    const allocator_type& __al = allocator_type())
00263       : _Base(__il, __al)
00264       { }
00265 
00266       ~forward_list() = default;
00267 
00268       forward_list&
00269       operator=(const forward_list&) = default;
00270 
00271       forward_list&
00272       operator=(forward_list&&) = default;
00273 
00274       forward_list&
00275       operator=(std::initializer_list<_Tp> __il)
00276       {
00277         _M_base() = __il;
00278         this->_M_invalidate_all();
00279         return *this;
00280       }
00281 
00282       template<typename _InputIterator,
00283                typename = std::_RequireInputIter<_InputIterator>>
00284         void
00285         assign(_InputIterator __first, _InputIterator __last)
00286         {
00287           typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
00288           __glibcxx_check_valid_range2(__first, __last, __dist);
00289 
00290           if (__dist.second >= __gnu_debug::__dp_sign)
00291             _Base::assign(__gnu_debug::__unsafe(__first),
00292                           __gnu_debug::__unsafe(__last));
00293           else
00294             _Base::assign(__first, __last);
00295 
00296           this->_M_invalidate_all();
00297         }
00298 
00299       void
00300       assign(size_type __n, const _Tp& __val)
00301       {
00302         _Base::assign(__n, __val);
00303         this->_M_invalidate_all();
00304       }
00305 
00306       void
00307       assign(std::initializer_list<_Tp> __il)
00308       {
00309         _Base::assign(__il);
00310         this->_M_invalidate_all();
00311       }
00312 
00313       using _Base::get_allocator;
00314 
00315       // iterators:
00316 
00317       iterator
00318       before_begin() noexcept
00319       { return { _Base::before_begin(), this }; }
00320 
00321       const_iterator
00322       before_begin() const noexcept
00323       { return { _Base::before_begin(), this }; }
00324 
00325       iterator
00326       begin() noexcept
00327       { return { _Base::begin(), this }; }
00328 
00329       const_iterator
00330       begin() const noexcept
00331       { return { _Base::begin(), this }; }
00332 
00333       iterator
00334       end() noexcept
00335       { return { _Base::end(), this }; }
00336 
00337       const_iterator
00338       end() const noexcept
00339       { return { _Base::end(), this }; }
00340 
00341       const_iterator
00342       cbegin() const noexcept
00343       { return { _Base::cbegin(), this }; }
00344 
00345       const_iterator
00346       cbefore_begin() const noexcept
00347       { return { _Base::cbefore_begin(), this }; }
00348 
00349       const_iterator
00350       cend() const noexcept
00351       { return { _Base::cend(), this }; }
00352 
00353       using _Base::empty;
00354       using _Base::max_size;
00355 
00356       // element access:
00357 
00358       reference
00359       front()
00360       {
00361         __glibcxx_check_nonempty();
00362         return _Base::front();
00363       }
00364 
00365       const_reference
00366       front() const
00367       {
00368         __glibcxx_check_nonempty();
00369         return _Base::front();
00370       }
00371 
00372       // modifiers:
00373 
00374       using _Base::emplace_front;
00375       using _Base::push_front;
00376 
00377       void
00378       pop_front()
00379       {
00380         __glibcxx_check_nonempty();
00381         this->_M_invalidate_if([this](_Base_const_iterator __it)
00382           { return __it == this->_M_base().cbegin(); });
00383         _Base::pop_front();
00384       }
00385 
00386       template<typename... _Args>
00387         iterator
00388         emplace_after(const_iterator __pos, _Args&&... __args)
00389         {
00390           __glibcxx_check_insert_after(__pos);
00391           return { _Base::emplace_after(__pos.base(),
00392                                         std::forward<_Args>(__args)...),
00393                    this };
00394         }
00395 
00396       iterator
00397       insert_after(const_iterator __pos, const _Tp& __val)
00398       {
00399         __glibcxx_check_insert_after(__pos);
00400         return { _Base::insert_after(__pos.base(), __val), this };
00401       }
00402 
00403       iterator
00404       insert_after(const_iterator __pos, _Tp&& __val)
00405       {
00406         __glibcxx_check_insert_after(__pos);
00407         return { _Base::insert_after(__pos.base(), std::move(__val)), this };
00408       }
00409 
00410       iterator
00411       insert_after(const_iterator __pos, size_type __n, const _Tp& __val)
00412       {
00413         __glibcxx_check_insert_after(__pos);
00414         return { _Base::insert_after(__pos.base(), __n, __val), this };
00415       }
00416 
00417       template<typename _InputIterator,
00418                typename = std::_RequireInputIter<_InputIterator>>
00419         iterator
00420         insert_after(const_iterator __pos,
00421                      _InputIterator __first, _InputIterator __last)
00422         {
00423           typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
00424           __glibcxx_check_insert_range_after(__pos, __first, __last, __dist);
00425 
00426           if (__dist.second >= __gnu_debug::__dp_sign)
00427             return
00428               {
00429                 _Base::insert_after(__pos.base(),
00430                                     __gnu_debug::__unsafe(__first),
00431                                     __gnu_debug::__unsafe(__last)),
00432                 this
00433               };
00434           else
00435             return { _Base::insert_after(__pos.base(), __first, __last), this };
00436         }
00437 
00438       iterator
00439       insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
00440       {
00441         __glibcxx_check_insert_after(__pos);
00442         return { _Base::insert_after(__pos.base(), __il), this };
00443       }
00444 
00445     private:
00446       _Base_iterator
00447       _M_erase_after(_Base_const_iterator __pos)
00448       {
00449         _Base_const_iterator __next = std::next(__pos);
00450         this->_M_invalidate_if([__next](_Base_const_iterator __it)
00451           { return __it == __next; });
00452         return _Base::erase_after(__pos);
00453       }
00454     public:
00455       iterator
00456       erase_after(const_iterator __pos)
00457       {
00458         __glibcxx_check_erase_after(__pos);
00459         return { _M_erase_after(__pos.base()), this };
00460       }
00461 
00462       iterator
00463       erase_after(const_iterator __pos, const_iterator __last)
00464       {
00465         __glibcxx_check_erase_range_after(__pos, __last);
00466         for (_Base_const_iterator __victim = std::next(__pos.base());
00467             __victim != __last.base(); ++__victim)
00468           {
00469             _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(),
00470                                   _M_message(__gnu_debug::__msg_valid_range2)
00471                                   ._M_sequence(*this, "this")
00472                                   ._M_iterator(__pos, "pos")
00473                                   ._M_iterator(__last, "last"));
00474             this->_M_invalidate_if([__victim](_Base_const_iterator __it)
00475               { return __it == __victim; });
00476           }
00477 
00478         return { _Base::erase_after(__pos.base(), __last.base()), this };
00479       }
00480 
00481       void
00482       swap(forward_list& __list)
00483         noexcept( noexcept(declval<_Base&>().swap(__list)) )
00484       {
00485         _Safe::_M_swap(__list);
00486         _Base::swap(__list);
00487       }
00488 
00489       void
00490       resize(size_type __sz)
00491       {
00492         this->_M_detach_singular();
00493 
00494         // if __sz < size(), invalidate all iterators in [begin+__sz, end()
00495         _Base_iterator __victim = _Base::begin();
00496         _Base_iterator __end = _Base::end();
00497         for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
00498           ++__victim;
00499 
00500         for (; __victim != __end; ++__victim)
00501           {
00502             this->_M_invalidate_if([__victim](_Base_const_iterator __it)
00503               { return __it == __victim; });
00504           }
00505 
00506         __try
00507           {
00508             _Base::resize(__sz);
00509           }
00510         __catch(...)
00511           {
00512             this->_M_revalidate_singular();
00513             __throw_exception_again;
00514           }
00515       }
00516 
00517       void
00518       resize(size_type __sz, const value_type& __val)
00519       {
00520         this->_M_detach_singular();
00521 
00522         // if __sz < size(), invalidate all iterators in [begin+__sz, end())
00523         _Base_iterator __victim = _Base::begin();
00524         _Base_iterator __end = _Base::end();
00525         for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
00526           ++__victim;
00527 
00528         for (; __victim != __end; ++__victim)
00529           {
00530             this->_M_invalidate_if([__victim](_Base_const_iterator __it)
00531               { return __it == __victim; });
00532           }
00533 
00534         __try
00535           {
00536             _Base::resize(__sz, __val);
00537           }
00538         __catch(...)
00539           {
00540             this->_M_revalidate_singular();
00541             __throw_exception_again;
00542           }
00543       }
00544 
00545       void
00546       clear() noexcept
00547       {
00548         _Base::clear();
00549         this->_M_invalidate_all();
00550       }
00551 
00552       // 23.2.3.5 forward_list operations:
00553       void
00554       splice_after(const_iterator __pos, forward_list&& __list)
00555       {
00556         __glibcxx_check_insert_after(__pos);
00557         _GLIBCXX_DEBUG_VERIFY(std::__addressof(__list) != this,
00558                               _M_message(__gnu_debug::__msg_self_splice)
00559                               ._M_sequence(*this, "this"));
00560         _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
00561                               _M_message(__gnu_debug::__msg_splice_alloc)
00562                               ._M_sequence(*this)
00563                               ._M_sequence(__list, "__list"));
00564         this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
00565           {
00566             return __it != __list._M_base().cbefore_begin()
00567                    && __it != __list._M_base().end();
00568           });
00569         _Base::splice_after(__pos.base(), std::move(__list._M_base()));
00570       }
00571 
00572       void
00573       splice_after(const_iterator __pos, forward_list& __list)
00574       { splice_after(__pos, std::move(__list)); }
00575 
00576       void
00577       splice_after(const_iterator __pos, forward_list&& __list,
00578                    const_iterator __i)
00579       {
00580         __glibcxx_check_insert_after(__pos);
00581         _GLIBCXX_DEBUG_VERIFY(__i._M_before_dereferenceable(),
00582                               _M_message(__gnu_debug::__msg_splice_bad)
00583                               ._M_iterator(__i, "__i"));
00584         _GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__list)),
00585                               _M_message(__gnu_debug::__msg_splice_other)
00586                               ._M_iterator(__i, "__i")
00587                               ._M_sequence(__list, "__list"));
00588         _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
00589                               _M_message(__gnu_debug::__msg_splice_alloc)
00590                               ._M_sequence(*this)
00591                               ._M_sequence(__list, "__list"));
00592 
00593         // _GLIBCXX_RESOLVE_LIB_DEFECTS
00594         // 250. splicing invalidates iterators
00595         _Base_const_iterator __next = std::next(__i.base());
00596         this->_M_transfer_from_if(__list, [__next](_Base_const_iterator __it)
00597           { return __it == __next; });
00598         _Base::splice_after(__pos.base(), std::move(__list._M_base()),
00599                             __i.base());
00600       }
00601 
00602       void
00603       splice_after(const_iterator __pos, forward_list& __list,
00604                    const_iterator __i)
00605       { splice_after(__pos, std::move(__list), __i); }
00606 
00607       void
00608       splice_after(const_iterator __pos, forward_list&& __list,
00609                    const_iterator __before, const_iterator __last)
00610       {
00611         typename __gnu_debug::_Distance_traits<const_iterator>::__type __dist;
00612         auto __listptr = std::__addressof(__list);
00613         __glibcxx_check_insert_after(__pos);
00614         __glibcxx_check_valid_fl_range(__before, __last, __dist);
00615         _GLIBCXX_DEBUG_VERIFY(__before._M_attached_to(__listptr),
00616                               _M_message(__gnu_debug::__msg_splice_other)
00617                               ._M_sequence(__list, "list")
00618                               ._M_iterator(__before, "before"));
00619         _GLIBCXX_DEBUG_VERIFY(__before._M_dereferenceable()
00620                               || __before._M_is_before_begin(),
00621                               _M_message(__gnu_debug::__msg_valid_range2)
00622                               ._M_sequence(__list, "list")
00623                               ._M_iterator(__before, "before")
00624                               ._M_iterator(__last, "last"));
00625         _GLIBCXX_DEBUG_VERIFY(__before != __last,
00626                               _M_message(__gnu_debug::__msg_valid_range2)
00627                               ._M_sequence(__list, "list")
00628                               ._M_iterator(__before, "before")
00629                               ._M_iterator(__last, "last"));
00630         _GLIBCXX_DEBUG_VERIFY(__list.get_allocator() == this->get_allocator(),
00631                               _M_message(__gnu_debug::__msg_splice_alloc)
00632                               ._M_sequence(*this)
00633                               ._M_sequence(__list, "__list"));
00634 
00635         for (_Base_const_iterator __tmp = std::next(__before.base());
00636              __tmp != __last.base(); ++__tmp)
00637           {
00638             _GLIBCXX_DEBUG_VERIFY(__tmp != __list._M_base().end(),
00639                                   _M_message(__gnu_debug::__msg_valid_range2)
00640                                   ._M_sequence(__list, "list")
00641                                   ._M_iterator(__before, "before")
00642                                   ._M_iterator(__last, "last"));
00643             _GLIBCXX_DEBUG_VERIFY(__listptr != this || __tmp != __pos.base(),
00644                                   _M_message(__gnu_debug::__msg_splice_overlap)
00645                                   ._M_iterator(__tmp, "position")
00646                                   ._M_iterator(__before, "before")
00647                                   ._M_iterator(__last, "last"));
00648             // _GLIBCXX_RESOLVE_LIB_DEFECTS
00649             // 250. splicing invalidates iterators
00650             this->_M_transfer_from_if(__list, [__tmp](_Base_const_iterator __it)
00651               { return __it == __tmp; });
00652           }
00653 
00654         _Base::splice_after(__pos.base(), std::move(__list._M_base()),
00655                             __before.base(), __last.base());
00656       }
00657 
00658       void
00659       splice_after(const_iterator __pos, forward_list& __list,
00660                    const_iterator __before, const_iterator __last)
00661       { splice_after(__pos, std::move(__list), __before, __last); }
00662 
00663     private:
00664 #if __cplusplus > 201703L
00665       using __remove_return_type = size_type;
00666 # define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG \
00667       __attribute__((__abi_tag__("__cxx20")))
00668 # define _GLIBCXX20_ONLY(__expr) __expr
00669 #else
00670       using __remove_return_type = void;
00671 # define _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
00672 # define _GLIBCXX20_ONLY(__expr)
00673 #endif
00674 
00675     public:
00676       _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
00677       __remove_return_type
00678       remove(const _Tp& __val)
00679       {
00680         if (!this->_M_iterators && !this->_M_const_iterators)
00681           return _Base::remove(__val);
00682 
00683         size_type __removed __attribute__((__unused__)) = 0;
00684         _Base_iterator __x = _Base::before_begin();
00685         _Base_iterator __old = __x++;
00686         _Base_iterator __extra = _Base::end();
00687         while (__x != _Base::end())
00688           {
00689             if (*__x == __val)
00690               {
00691                 if (std::__addressof(*__x) != std::__addressof(__val))
00692                   {
00693                     __x = _M_erase_after(__old);
00694                     _GLIBCXX20_ONLY( __removed++ );
00695                     continue;
00696                   }
00697                 else
00698                   __extra = __old;
00699               }
00700             __old = __x++;
00701           }
00702 
00703         if (__extra != _Base::end())
00704           {
00705             this->_M_erase_after(__extra);
00706             _GLIBCXX20_ONLY( __removed++ );
00707           }
00708 
00709         return _GLIBCXX20_ONLY( __removed );
00710       }
00711 
00712       template<typename _Pred>
00713         __remove_return_type
00714         remove_if(_Pred __pred)
00715         {
00716           if (!this->_M_iterators && !this->_M_const_iterators)
00717             return _Base::remove_if(__pred);
00718 
00719           size_type __removed __attribute__((__unused__)) = 0;
00720           _Base_iterator __x = _Base::before_begin();
00721           _Base_iterator __old = __x++;
00722           while (__x != _Base::end())
00723             if (__pred(*__x))
00724               {
00725                 __x = _M_erase_after(__old);
00726                 _GLIBCXX20_ONLY( __removed++ );
00727               }
00728             else
00729               __old = __x++;
00730 
00731           return _GLIBCXX20_ONLY( __removed );
00732         }
00733 
00734       _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
00735       __remove_return_type
00736       unique()
00737       { return unique(std::equal_to<_Tp>()); }
00738 
00739       template<typename _BinPred>
00740         __remove_return_type
00741         unique(_BinPred __binary_pred)
00742         {
00743           if (!this->_M_iterators && !this->_M_const_iterators)
00744             return _Base::unique(__binary_pred);
00745 
00746           _Base_iterator __first = _Base::begin();
00747           _Base_iterator __last = _Base::end();
00748           if (__first == __last)
00749             return _GLIBCXX20_ONLY(0);
00750 
00751           size_type __removed __attribute__((__unused__)) = 0;
00752           _Base_iterator __next = std::next(__first);
00753           while (__next != __last)
00754             {
00755               if (__binary_pred(*__first, *__next))
00756                 {
00757                   __next = _M_erase_after(__first);
00758                   _GLIBCXX20_ONLY( __removed++ );
00759                 }
00760               else
00761                 __first = __next++;
00762             }
00763 
00764           return _GLIBCXX20_ONLY( __removed );
00765         }
00766 
00767 #undef _GLIBCXX_FWDLIST_REMOVE_RETURN_TYPE_TAG
00768 #undef _GLIBCXX20_ONLY
00769 
00770       void
00771       merge(forward_list&& __list)
00772       {
00773         if (this != std::__addressof(__list))
00774         {
00775           __glibcxx_check_sorted(_Base::begin(), _Base::end());
00776           __glibcxx_check_sorted(__list._M_base().begin(),
00777                                  __list._M_base().end());
00778           this->_M_transfer_from_if(__list, [&__list](_Base_const_iterator __it)
00779             {
00780               return __it != __list._M_base().cbefore_begin()
00781                      && __it != __list._M_base().cend();
00782             });
00783           _Base::merge(std::move(__list._M_base()));
00784         }
00785       }
00786 
00787       void
00788       merge(forward_list& __list)
00789       { merge(std::move(__list)); }
00790 
00791       template<typename _Comp>
00792         void
00793         merge(forward_list&& __list, _Comp __comp)
00794         {
00795           if (this != std::__addressof(__list))
00796           {
00797             __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(), __comp);
00798             __glibcxx_check_sorted_pred(__list._M_base().begin(),
00799                                         __list._M_base().end(), __comp);
00800             this->_M_transfer_from_if(__list,
00801                                       [&__list](_Base_const_iterator __it)
00802               {
00803                 return __it != __list._M_base().cbefore_begin()
00804                        && __it != __list._M_base().cend();
00805               });
00806             _Base::merge(std::move(__list._M_base()), __comp);
00807           }
00808         }
00809 
00810       template<typename _Comp>
00811         void
00812         merge(forward_list& __list, _Comp __comp)
00813         { merge(std::move(__list), __comp); }
00814 
00815       using _Base::sort;
00816       using _Base::reverse;
00817 
00818       _Base&
00819       _M_base() noexcept { return *this; }
00820 
00821       const _Base&
00822       _M_base() const noexcept { return *this; }
00823     };
00824 
00825 #if __cpp_deduction_guides >= 201606
00826   template<typename _InputIterator, typename _ValT
00827              = typename iterator_traits<_InputIterator>::value_type,
00828            typename _Allocator = allocator<_ValT>,
00829            typename = _RequireInputIter<_InputIterator>,
00830            typename = _RequireAllocator<_Allocator>>
00831     forward_list(_InputIterator, _InputIterator, _Allocator = _Allocator())
00832       -> forward_list<_ValT, _Allocator>;
00833 #endif
00834 
00835   template<typename _Tp, typename _Alloc>
00836     bool
00837     operator==(const forward_list<_Tp, _Alloc>& __lx,
00838                const forward_list<_Tp, _Alloc>& __ly)
00839     { return __lx._M_base() == __ly._M_base(); }
00840 
00841   template<typename _Tp, typename _Alloc>
00842     inline bool
00843     operator<(const forward_list<_Tp, _Alloc>& __lx,
00844               const forward_list<_Tp, _Alloc>& __ly)
00845     { return __lx._M_base() < __ly._M_base(); }
00846 
00847   template<typename _Tp, typename _Alloc>
00848     inline bool
00849     operator!=(const forward_list<_Tp, _Alloc>& __lx,
00850                const forward_list<_Tp, _Alloc>& __ly)
00851     { return !(__lx == __ly); }
00852 
00853   /// Based on operator<
00854   template<typename _Tp, typename _Alloc>
00855     inline bool
00856     operator>(const forward_list<_Tp, _Alloc>& __lx,
00857               const forward_list<_Tp, _Alloc>& __ly)
00858     { return (__ly < __lx); }
00859 
00860   /// Based on operator<
00861   template<typename _Tp, typename _Alloc>
00862     inline bool
00863     operator>=(const forward_list<_Tp, _Alloc>& __lx,
00864                const forward_list<_Tp, _Alloc>& __ly)
00865     { return !(__lx < __ly); }
00866 
00867   /// Based on operator<
00868   template<typename _Tp, typename _Alloc>
00869     inline bool
00870     operator<=(const forward_list<_Tp, _Alloc>& __lx,
00871                const forward_list<_Tp, _Alloc>& __ly)
00872     { return !(__ly < __lx); }
00873 
00874   /// See std::forward_list::swap().
00875   template<typename _Tp, typename _Alloc>
00876     inline void
00877     swap(forward_list<_Tp, _Alloc>& __lx, forward_list<_Tp, _Alloc>& __ly)
00878     noexcept(noexcept(__lx.swap(__ly)))
00879     { __lx.swap(__ly); }
00880 
00881 } // namespace __debug
00882 } // namespace std
00883 
00884 namespace __gnu_debug
00885 {
00886   template<typename _Tp, typename _Alloc>
00887     struct _BeforeBeginHelper<std::__debug::forward_list<_Tp, _Alloc> >
00888     {
00889       typedef std::__debug::forward_list<_Tp, _Alloc> _Sequence;
00890 
00891       template<typename _Iterator>
00892         static bool
00893         _S_Is(const _Safe_iterator<_Iterator, _Sequence>& __it)
00894         {
00895           return
00896             __it.base() == __it._M_get_sequence()->_M_base().before_begin();
00897         }
00898 
00899       template<typename _Iterator>
00900         static bool
00901         _S_Is_Beginnest(const _Safe_iterator<_Iterator, _Sequence>& __it)
00902         { return _S_Is(__it); }
00903     };
00904 
00905   template<typename _Tp, typename _Alloc>
00906     struct _Sequence_traits<std::__debug::forward_list<_Tp, _Alloc> >
00907     {
00908       typedef typename std::__debug::forward_list<_Tp, _Alloc>::iterator _It;
00909 
00910       static typename _Distance_traits<_It>::__type
00911       _S_size(const std::__debug::forward_list<_Tp, _Alloc>& __seq)
00912       {
00913         return __seq.empty()
00914           ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_equality);
00915       }
00916     };
00917 
00918 #ifndef _GLIBCXX_DEBUG_PEDANTIC
00919   template<class _Tp, class _Alloc>
00920     struct _Insert_range_from_self_is_safe<
00921       std::__debug::forward_list<_Tp, _Alloc> >
00922     { enum { __value = 1 }; };
00923 #endif
00924 }
00925 
00926 #endif