PR libstdc++/48101 improve errors for invalid container specializations
[official-gcc.git] / libstdc++-v3 / include / bits / forward_list.h
blob96494cc20de574d6b319394104bb2b04700e5b6e
1 // <forward_list.h> -*- C++ -*-
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/forward_list.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{forward_list}
30 #ifndef _FORWARD_LIST_H
31 #define _FORWARD_LIST_H 1
33 #pragma GCC system_header
35 #include <initializer_list>
36 #include <bits/stl_iterator_base_types.h>
37 #include <bits/stl_iterator.h>
38 #include <bits/stl_algobase.h>
39 #include <bits/stl_function.h>
40 #include <bits/allocator.h>
41 #include <ext/alloc_traits.h>
42 #include <ext/aligned_buffer.h>
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
49 /**
50 * @brief A helper basic node class for %forward_list.
51 * This is just a linked list with nothing inside it.
52 * There are purely list shuffling utility methods here.
54 struct _Fwd_list_node_base
56 _Fwd_list_node_base() = default;
58 _Fwd_list_node_base* _M_next = nullptr;
60 _Fwd_list_node_base*
61 _M_transfer_after(_Fwd_list_node_base* __begin,
62 _Fwd_list_node_base* __end) noexcept
64 _Fwd_list_node_base* __keep = __begin->_M_next;
65 if (__end)
67 __begin->_M_next = __end->_M_next;
68 __end->_M_next = _M_next;
70 else
71 __begin->_M_next = 0;
72 _M_next = __keep;
73 return __end;
76 void
77 _M_reverse_after() noexcept
79 _Fwd_list_node_base* __tail = _M_next;
80 if (!__tail)
81 return;
82 while (_Fwd_list_node_base* __temp = __tail->_M_next)
84 _Fwd_list_node_base* __keep = _M_next;
85 _M_next = __temp;
86 __tail->_M_next = __temp->_M_next;
87 _M_next->_M_next = __keep;
92 /**
93 * @brief A helper node class for %forward_list.
94 * This is just a linked list with uninitialized storage for a
95 * data value in each node.
96 * There is a sorting utility method.
98 template<typename _Tp>
99 struct _Fwd_list_node
100 : public _Fwd_list_node_base
102 _Fwd_list_node() = default;
104 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
106 _Tp*
107 _M_valptr() noexcept
108 { return _M_storage._M_ptr(); }
110 const _Tp*
111 _M_valptr() const noexcept
112 { return _M_storage._M_ptr(); }
116 * @brief A forward_list::iterator.
118 * All the functions are op overloads.
120 template<typename _Tp>
121 struct _Fwd_list_iterator
123 typedef _Fwd_list_iterator<_Tp> _Self;
124 typedef _Fwd_list_node<_Tp> _Node;
126 typedef _Tp value_type;
127 typedef _Tp* pointer;
128 typedef _Tp& reference;
129 typedef ptrdiff_t difference_type;
130 typedef std::forward_iterator_tag iterator_category;
132 _Fwd_list_iterator() noexcept
133 : _M_node() { }
135 explicit
136 _Fwd_list_iterator(_Fwd_list_node_base* __n) noexcept
137 : _M_node(__n) { }
139 reference
140 operator*() const noexcept
141 { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); }
143 pointer
144 operator->() const noexcept
145 { return static_cast<_Node*>(this->_M_node)->_M_valptr(); }
147 _Self&
148 operator++() noexcept
150 _M_node = _M_node->_M_next;
151 return *this;
154 _Self
155 operator++(int) noexcept
157 _Self __tmp(*this);
158 _M_node = _M_node->_M_next;
159 return __tmp;
162 bool
163 operator==(const _Self& __x) const noexcept
164 { return _M_node == __x._M_node; }
166 bool
167 operator!=(const _Self& __x) const noexcept
168 { return _M_node != __x._M_node; }
170 _Self
171 _M_next() const noexcept
173 if (_M_node)
174 return _Fwd_list_iterator(_M_node->_M_next);
175 else
176 return _Fwd_list_iterator(0);
179 _Fwd_list_node_base* _M_node;
183 * @brief A forward_list::const_iterator.
185 * All the functions are op overloads.
187 template<typename _Tp>
188 struct _Fwd_list_const_iterator
190 typedef _Fwd_list_const_iterator<_Tp> _Self;
191 typedef const _Fwd_list_node<_Tp> _Node;
192 typedef _Fwd_list_iterator<_Tp> iterator;
194 typedef _Tp value_type;
195 typedef const _Tp* pointer;
196 typedef const _Tp& reference;
197 typedef ptrdiff_t difference_type;
198 typedef std::forward_iterator_tag iterator_category;
200 _Fwd_list_const_iterator() noexcept
201 : _M_node() { }
203 explicit
204 _Fwd_list_const_iterator(const _Fwd_list_node_base* __n) noexcept
205 : _M_node(__n) { }
207 _Fwd_list_const_iterator(const iterator& __iter) noexcept
208 : _M_node(__iter._M_node) { }
210 reference
211 operator*() const noexcept
212 { return *static_cast<_Node*>(this->_M_node)->_M_valptr(); }
214 pointer
215 operator->() const noexcept
216 { return static_cast<_Node*>(this->_M_node)->_M_valptr(); }
218 _Self&
219 operator++() noexcept
221 _M_node = _M_node->_M_next;
222 return *this;
225 _Self
226 operator++(int) noexcept
228 _Self __tmp(*this);
229 _M_node = _M_node->_M_next;
230 return __tmp;
233 bool
234 operator==(const _Self& __x) const noexcept
235 { return _M_node == __x._M_node; }
237 bool
238 operator!=(const _Self& __x) const noexcept
239 { return _M_node != __x._M_node; }
241 _Self
242 _M_next() const noexcept
244 if (this->_M_node)
245 return _Fwd_list_const_iterator(_M_node->_M_next);
246 else
247 return _Fwd_list_const_iterator(0);
250 const _Fwd_list_node_base* _M_node;
254 * @brief Forward list iterator equality comparison.
256 template<typename _Tp>
257 inline bool
258 operator==(const _Fwd_list_iterator<_Tp>& __x,
259 const _Fwd_list_const_iterator<_Tp>& __y) noexcept
260 { return __x._M_node == __y._M_node; }
263 * @brief Forward list iterator inequality comparison.
265 template<typename _Tp>
266 inline bool
267 operator!=(const _Fwd_list_iterator<_Tp>& __x,
268 const _Fwd_list_const_iterator<_Tp>& __y) noexcept
269 { return __x._M_node != __y._M_node; }
272 * @brief Base class for %forward_list.
274 template<typename _Tp, typename _Alloc>
275 struct _Fwd_list_base
277 protected:
278 typedef __alloc_rebind<_Alloc, _Tp> _Tp_alloc_type;
279 typedef __alloc_rebind<_Alloc, _Fwd_list_node<_Tp>> _Node_alloc_type;
280 typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits;
282 struct _Fwd_list_impl
283 : public _Node_alloc_type
285 _Fwd_list_node_base _M_head;
287 _Fwd_list_impl()
288 : _Node_alloc_type(), _M_head()
291 _Fwd_list_impl(const _Node_alloc_type& __a)
292 : _Node_alloc_type(__a), _M_head()
295 _Fwd_list_impl(_Node_alloc_type&& __a)
296 : _Node_alloc_type(std::move(__a)), _M_head()
300 _Fwd_list_impl _M_impl;
302 public:
303 typedef _Fwd_list_iterator<_Tp> iterator;
304 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
305 typedef _Fwd_list_node<_Tp> _Node;
307 _Node_alloc_type&
308 _M_get_Node_allocator() noexcept
309 { return this->_M_impl; }
311 const _Node_alloc_type&
312 _M_get_Node_allocator() const noexcept
313 { return this->_M_impl; }
315 _Fwd_list_base()
316 : _M_impl() { }
318 _Fwd_list_base(_Node_alloc_type&& __a)
319 : _M_impl(std::move(__a)) { }
321 _Fwd_list_base(_Fwd_list_base&& __lst, _Node_alloc_type&& __a);
323 _Fwd_list_base(_Fwd_list_base&& __lst)
324 : _M_impl(std::move(__lst._M_get_Node_allocator()))
326 this->_M_impl._M_head._M_next = __lst._M_impl._M_head._M_next;
327 __lst._M_impl._M_head._M_next = 0;
330 ~_Fwd_list_base()
331 { _M_erase_after(&_M_impl._M_head, 0); }
333 protected:
335 _Node*
336 _M_get_node()
338 auto __ptr = _Node_alloc_traits::allocate(_M_get_Node_allocator(), 1);
339 return std::__to_address(__ptr);
342 template<typename... _Args>
343 _Node*
344 _M_create_node(_Args&&... __args)
346 _Node* __node = this->_M_get_node();
347 __try
349 _Tp_alloc_type __a(_M_get_Node_allocator());
350 typedef allocator_traits<_Tp_alloc_type> _Alloc_traits;
351 ::new ((void*)__node) _Node;
352 _Alloc_traits::construct(__a, __node->_M_valptr(),
353 std::forward<_Args>(__args)...);
355 __catch(...)
357 this->_M_put_node(__node);
358 __throw_exception_again;
360 return __node;
363 template<typename... _Args>
364 _Fwd_list_node_base*
365 _M_insert_after(const_iterator __pos, _Args&&... __args);
367 void
368 _M_put_node(_Node* __p)
370 typedef typename _Node_alloc_traits::pointer _Ptr;
371 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__p);
372 _Node_alloc_traits::deallocate(_M_get_Node_allocator(), __ptr, 1);
375 _Fwd_list_node_base*
376 _M_erase_after(_Fwd_list_node_base* __pos);
378 _Fwd_list_node_base*
379 _M_erase_after(_Fwd_list_node_base* __pos,
380 _Fwd_list_node_base* __last);
384 * @brief A standard container with linear time access to elements,
385 * and fixed time insertion/deletion at any point in the sequence.
387 * @ingroup sequences
389 * @tparam _Tp Type of element.
390 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
392 * Meets the requirements of a <a href="tables.html#65">container</a>, a
393 * <a href="tables.html#67">sequence</a>, including the
394 * <a href="tables.html#68">optional sequence requirements</a> with the
395 * %exception of @c at and @c operator[].
397 * This is a @e singly @e linked %list. Traversal up the
398 * %list requires linear time, but adding and removing elements (or
399 * @e nodes) is done in constant time, regardless of where the
400 * change takes place. Unlike std::vector and std::deque,
401 * random-access iterators are not provided, so subscripting ( @c
402 * [] ) access is not allowed. For algorithms which only need
403 * sequential access, this lack makes no difference.
405 * Also unlike the other standard containers, std::forward_list provides
406 * specialized algorithms %unique to linked lists, such as
407 * splicing, sorting, and in-place reversal.
409 template<typename _Tp, typename _Alloc = allocator<_Tp>>
410 class forward_list : private _Fwd_list_base<_Tp, _Alloc>
412 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
413 "std::forward_list must have a non-const, non-volatile value_type");
414 #ifdef __STRICT_ANSI__
415 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
416 "std::forward_list must have the same value_type as its allocator");
417 #endif
419 private:
420 typedef _Fwd_list_base<_Tp, _Alloc> _Base;
421 typedef _Fwd_list_node<_Tp> _Node;
422 typedef _Fwd_list_node_base _Node_base;
423 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
424 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
425 typedef typename _Base::_Node_alloc_traits _Node_alloc_traits;
426 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
428 public:
429 // types:
430 typedef _Tp value_type;
431 typedef typename _Alloc_traits::pointer pointer;
432 typedef typename _Alloc_traits::const_pointer const_pointer;
433 typedef value_type& reference;
434 typedef const value_type& const_reference;
436 typedef _Fwd_list_iterator<_Tp> iterator;
437 typedef _Fwd_list_const_iterator<_Tp> const_iterator;
438 typedef std::size_t size_type;
439 typedef std::ptrdiff_t difference_type;
440 typedef _Alloc allocator_type;
442 // 23.3.4.2 construct/copy/destroy:
445 * @brief Creates a %forward_list with no elements.
447 forward_list()
448 noexcept(is_nothrow_default_constructible<_Node_alloc_type>::value)
449 : _Base()
453 * @brief Creates a %forward_list with no elements.
454 * @param __al An allocator object.
456 explicit
457 forward_list(const _Alloc& __al) noexcept
458 : _Base(_Node_alloc_type(__al))
463 * @brief Copy constructor with allocator argument.
464 * @param __list Input list to copy.
465 * @param __al An allocator object.
467 forward_list(const forward_list& __list, const _Alloc& __al)
468 : _Base(_Node_alloc_type(__al))
469 { _M_range_initialize(__list.begin(), __list.end()); }
472 * @brief Move constructor with allocator argument.
473 * @param __list Input list to move.
474 * @param __al An allocator object.
476 forward_list(forward_list&& __list, const _Alloc& __al)
477 noexcept(_Node_alloc_traits::_S_always_equal())
478 : _Base(std::move(__list), _Node_alloc_type(__al))
480 // If __list is not empty it means its allocator is not equal to __a,
481 // so we need to move from each element individually.
482 insert_after(cbefore_begin(),
483 std::__make_move_if_noexcept_iterator(__list.begin()),
484 std::__make_move_if_noexcept_iterator(__list.end()));
488 * @brief Creates a %forward_list with default constructed elements.
489 * @param __n The number of elements to initially create.
490 * @param __al An allocator object.
492 * This constructor creates the %forward_list with @a __n default
493 * constructed elements.
495 explicit
496 forward_list(size_type __n, const _Alloc& __al = _Alloc())
497 : _Base(_Node_alloc_type(__al))
498 { _M_default_initialize(__n); }
501 * @brief Creates a %forward_list with copies of an exemplar element.
502 * @param __n The number of elements to initially create.
503 * @param __value An element to copy.
504 * @param __al An allocator object.
506 * This constructor fills the %forward_list with @a __n copies of
507 * @a __value.
509 forward_list(size_type __n, const _Tp& __value,
510 const _Alloc& __al = _Alloc())
511 : _Base(_Node_alloc_type(__al))
512 { _M_fill_initialize(__n, __value); }
515 * @brief Builds a %forward_list from a range.
516 * @param __first An input iterator.
517 * @param __last An input iterator.
518 * @param __al An allocator object.
520 * Create a %forward_list consisting of copies of the elements from
521 * [@a __first,@a __last). This is linear in N (where N is
522 * distance(@a __first,@a __last)).
524 template<typename _InputIterator,
525 typename = std::_RequireInputIter<_InputIterator>>
526 forward_list(_InputIterator __first, _InputIterator __last,
527 const _Alloc& __al = _Alloc())
528 : _Base(_Node_alloc_type(__al))
529 { _M_range_initialize(__first, __last); }
532 * @brief The %forward_list copy constructor.
533 * @param __list A %forward_list of identical element and allocator
534 * types.
536 forward_list(const forward_list& __list)
537 : _Base(_Node_alloc_traits::_S_select_on_copy(
538 __list._M_get_Node_allocator()))
539 { _M_range_initialize(__list.begin(), __list.end()); }
542 * @brief The %forward_list move constructor.
543 * @param __list A %forward_list of identical element and allocator
544 * types.
546 * The newly-created %forward_list contains the exact contents of @a
547 * __list. The contents of @a __list are a valid, but unspecified
548 * %forward_list.
550 forward_list(forward_list&& __list) noexcept
551 : _Base(std::move(__list)) { }
554 * @brief Builds a %forward_list from an initializer_list
555 * @param __il An initializer_list of value_type.
556 * @param __al An allocator object.
558 * Create a %forward_list consisting of copies of the elements
559 * in the initializer_list @a __il. This is linear in __il.size().
561 forward_list(std::initializer_list<_Tp> __il,
562 const _Alloc& __al = _Alloc())
563 : _Base(_Node_alloc_type(__al))
564 { _M_range_initialize(__il.begin(), __il.end()); }
567 * @brief The forward_list dtor.
569 ~forward_list() noexcept
573 * @brief The %forward_list assignment operator.
574 * @param __list A %forward_list of identical element and allocator
575 * types.
577 * All the elements of @a __list are copied.
579 * Whether the allocator is copied depends on the allocator traits.
581 forward_list&
582 operator=(const forward_list& __list);
585 * @brief The %forward_list move assignment operator.
586 * @param __list A %forward_list of identical element and allocator
587 * types.
589 * The contents of @a __list are moved into this %forward_list
590 * (without copying, if the allocators permit it).
592 * Afterwards @a __list is a valid, but unspecified %forward_list
594 * Whether the allocator is moved depends on the allocator traits.
596 forward_list&
597 operator=(forward_list&& __list)
598 noexcept(_Node_alloc_traits::_S_nothrow_move())
600 constexpr bool __move_storage =
601 _Node_alloc_traits::_S_propagate_on_move_assign()
602 || _Node_alloc_traits::_S_always_equal();
603 _M_move_assign(std::move(__list), __bool_constant<__move_storage>());
604 return *this;
608 * @brief The %forward_list initializer list assignment operator.
609 * @param __il An initializer_list of value_type.
611 * Replace the contents of the %forward_list with copies of the
612 * elements in the initializer_list @a __il. This is linear in
613 * __il.size().
615 forward_list&
616 operator=(std::initializer_list<_Tp> __il)
618 assign(__il);
619 return *this;
623 * @brief Assigns a range to a %forward_list.
624 * @param __first An input iterator.
625 * @param __last An input iterator.
627 * This function fills a %forward_list with copies of the elements
628 * in the range [@a __first,@a __last).
630 * Note that the assignment completely changes the %forward_list and
631 * that the number of elements of the resulting %forward_list is the
632 * same as the number of elements assigned.
634 template<typename _InputIterator,
635 typename = std::_RequireInputIter<_InputIterator>>
636 void
637 assign(_InputIterator __first, _InputIterator __last)
639 typedef is_assignable<_Tp, decltype(*__first)> __assignable;
640 _M_assign(__first, __last, __assignable());
644 * @brief Assigns a given value to a %forward_list.
645 * @param __n Number of elements to be assigned.
646 * @param __val Value to be assigned.
648 * This function fills a %forward_list with @a __n copies of the
649 * given value. Note that the assignment completely changes the
650 * %forward_list, and that the resulting %forward_list has __n
651 * elements.
653 void
654 assign(size_type __n, const _Tp& __val)
655 { _M_assign_n(__n, __val, is_copy_assignable<_Tp>()); }
658 * @brief Assigns an initializer_list to a %forward_list.
659 * @param __il An initializer_list of value_type.
661 * Replace the contents of the %forward_list with copies of the
662 * elements in the initializer_list @a __il. This is linear in
663 * il.size().
665 void
666 assign(std::initializer_list<_Tp> __il)
667 { assign(__il.begin(), __il.end()); }
669 /// Get a copy of the memory allocation object.
670 allocator_type
671 get_allocator() const noexcept
672 { return allocator_type(this->_M_get_Node_allocator()); }
674 // 23.3.4.3 iterators:
677 * Returns a read/write iterator that points before the first element
678 * in the %forward_list. Iteration is done in ordinary element order.
680 iterator
681 before_begin() noexcept
682 { return iterator(&this->_M_impl._M_head); }
685 * Returns a read-only (constant) iterator that points before the
686 * first element in the %forward_list. Iteration is done in ordinary
687 * element order.
689 const_iterator
690 before_begin() const noexcept
691 { return const_iterator(&this->_M_impl._M_head); }
694 * Returns a read/write iterator that points to the first element
695 * in the %forward_list. Iteration is done in ordinary element order.
697 iterator
698 begin() noexcept
699 { return iterator(this->_M_impl._M_head._M_next); }
702 * Returns a read-only (constant) iterator that points to the first
703 * element in the %forward_list. Iteration is done in ordinary
704 * element order.
706 const_iterator
707 begin() const noexcept
708 { return const_iterator(this->_M_impl._M_head._M_next); }
711 * Returns a read/write iterator that points one past the last
712 * element in the %forward_list. Iteration is done in ordinary
713 * element order.
715 iterator
716 end() noexcept
717 { return iterator(0); }
720 * Returns a read-only iterator that points one past the last
721 * element in the %forward_list. Iteration is done in ordinary
722 * element order.
724 const_iterator
725 end() const noexcept
726 { return const_iterator(0); }
729 * Returns a read-only (constant) iterator that points to the
730 * first element in the %forward_list. Iteration is done in ordinary
731 * element order.
733 const_iterator
734 cbegin() const noexcept
735 { return const_iterator(this->_M_impl._M_head._M_next); }
738 * Returns a read-only (constant) iterator that points before the
739 * first element in the %forward_list. Iteration is done in ordinary
740 * element order.
742 const_iterator
743 cbefore_begin() const noexcept
744 { return const_iterator(&this->_M_impl._M_head); }
747 * Returns a read-only (constant) iterator that points one past
748 * the last element in the %forward_list. Iteration is done in
749 * ordinary element order.
751 const_iterator
752 cend() const noexcept
753 { return const_iterator(0); }
756 * Returns true if the %forward_list is empty. (Thus begin() would
757 * equal end().)
759 bool
760 empty() const noexcept
761 { return this->_M_impl._M_head._M_next == 0; }
764 * Returns the largest possible number of elements of %forward_list.
766 size_type
767 max_size() const noexcept
768 { return _Node_alloc_traits::max_size(this->_M_get_Node_allocator()); }
770 // 23.3.4.4 element access:
773 * Returns a read/write reference to the data at the first
774 * element of the %forward_list.
776 reference
777 front()
779 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
780 return *__front->_M_valptr();
784 * Returns a read-only (constant) reference to the data at the first
785 * element of the %forward_list.
787 const_reference
788 front() const
790 _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
791 return *__front->_M_valptr();
794 // 23.3.4.5 modifiers:
797 * @brief Constructs object in %forward_list at the front of the
798 * list.
799 * @param __args Arguments.
801 * This function will insert an object of type Tp constructed
802 * with Tp(std::forward<Args>(args)...) at the front of the list
803 * Due to the nature of a %forward_list this operation can
804 * be done in constant time, and does not invalidate iterators
805 * and references.
807 template<typename... _Args>
808 #if __cplusplus > 201402L
809 reference
810 #else
811 void
812 #endif
813 emplace_front(_Args&&... __args)
815 this->_M_insert_after(cbefore_begin(),
816 std::forward<_Args>(__args)...);
817 #if __cplusplus > 201402L
818 return front();
819 #endif
823 * @brief Add data to the front of the %forward_list.
824 * @param __val Data to be added.
826 * This is a typical stack operation. The function creates an
827 * element at the front of the %forward_list and assigns the given
828 * data to it. Due to the nature of a %forward_list this operation
829 * can be done in constant time, and does not invalidate iterators
830 * and references.
832 void
833 push_front(const _Tp& __val)
834 { this->_M_insert_after(cbefore_begin(), __val); }
839 void
840 push_front(_Tp&& __val)
841 { this->_M_insert_after(cbefore_begin(), std::move(__val)); }
844 * @brief Removes first element.
846 * This is a typical stack operation. It shrinks the %forward_list
847 * by one. Due to the nature of a %forward_list this operation can
848 * be done in constant time, and only invalidates iterators/references
849 * to the element being removed.
851 * Note that no data is returned, and if the first element's data
852 * is needed, it should be retrieved before pop_front() is
853 * called.
855 void
856 pop_front()
857 { this->_M_erase_after(&this->_M_impl._M_head); }
860 * @brief Constructs object in %forward_list after the specified
861 * iterator.
862 * @param __pos A const_iterator into the %forward_list.
863 * @param __args Arguments.
864 * @return An iterator that points to the inserted data.
866 * This function will insert an object of type T constructed
867 * with T(std::forward<Args>(args)...) after the specified
868 * location. Due to the nature of a %forward_list this operation can
869 * be done in constant time, and does not invalidate iterators
870 * and references.
872 template<typename... _Args>
873 iterator
874 emplace_after(const_iterator __pos, _Args&&... __args)
875 { return iterator(this->_M_insert_after(__pos,
876 std::forward<_Args>(__args)...)); }
879 * @brief Inserts given value into %forward_list after specified
880 * iterator.
881 * @param __pos An iterator into the %forward_list.
882 * @param __val Data to be inserted.
883 * @return An iterator that points to the inserted data.
885 * This function will insert a copy of the given value after
886 * the specified location. Due to the nature of a %forward_list this
887 * operation can be done in constant time, and does not
888 * invalidate iterators and references.
890 iterator
891 insert_after(const_iterator __pos, const _Tp& __val)
892 { return iterator(this->_M_insert_after(__pos, __val)); }
897 iterator
898 insert_after(const_iterator __pos, _Tp&& __val)
899 { return iterator(this->_M_insert_after(__pos, std::move(__val))); }
902 * @brief Inserts a number of copies of given data into the
903 * %forward_list.
904 * @param __pos An iterator into the %forward_list.
905 * @param __n Number of elements to be inserted.
906 * @param __val Data to be inserted.
907 * @return An iterator pointing to the last inserted copy of
908 * @a val or @a pos if @a n == 0.
910 * This function will insert a specified number of copies of the
911 * given data after the location specified by @a pos.
913 * This operation is linear in the number of elements inserted and
914 * does not invalidate iterators and references.
916 iterator
917 insert_after(const_iterator __pos, size_type __n, const _Tp& __val);
920 * @brief Inserts a range into the %forward_list.
921 * @param __pos An iterator into the %forward_list.
922 * @param __first An input iterator.
923 * @param __last An input iterator.
924 * @return An iterator pointing to the last inserted element or
925 * @a __pos if @a __first == @a __last.
927 * This function will insert copies of the data in the range
928 * [@a __first,@a __last) into the %forward_list after the
929 * location specified by @a __pos.
931 * This operation is linear in the number of elements inserted and
932 * does not invalidate iterators and references.
934 template<typename _InputIterator,
935 typename = std::_RequireInputIter<_InputIterator>>
936 iterator
937 insert_after(const_iterator __pos,
938 _InputIterator __first, _InputIterator __last);
941 * @brief Inserts the contents of an initializer_list into
942 * %forward_list after the specified iterator.
943 * @param __pos An iterator into the %forward_list.
944 * @param __il An initializer_list of value_type.
945 * @return An iterator pointing to the last inserted element
946 * or @a __pos if @a __il is empty.
948 * This function will insert copies of the data in the
949 * initializer_list @a __il into the %forward_list before the location
950 * specified by @a __pos.
952 * This operation is linear in the number of elements inserted and
953 * does not invalidate iterators and references.
955 iterator
956 insert_after(const_iterator __pos, std::initializer_list<_Tp> __il)
957 { return insert_after(__pos, __il.begin(), __il.end()); }
960 * @brief Removes the element pointed to by the iterator following
961 * @c pos.
962 * @param __pos Iterator pointing before element to be erased.
963 * @return An iterator pointing to the element following the one
964 * that was erased, or end() if no such element exists.
966 * This function will erase the element at the given position and
967 * thus shorten the %forward_list by one.
969 * Due to the nature of a %forward_list this operation can be done
970 * in constant time, and only invalidates iterators/references to
971 * the element being removed. The user is also cautioned that
972 * this function only erases the element, and that if the element
973 * is itself a pointer, the pointed-to memory is not touched in
974 * any way. Managing the pointer is the user's responsibility.
976 iterator
977 erase_after(const_iterator __pos)
978 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
979 (__pos._M_node))); }
982 * @brief Remove a range of elements.
983 * @param __pos Iterator pointing before the first element to be
984 * erased.
985 * @param __last Iterator pointing to one past the last element to be
986 * erased.
987 * @return @ __last.
989 * This function will erase the elements in the range
990 * @a (__pos,__last) and shorten the %forward_list accordingly.
992 * This operation is linear time in the size of the range and only
993 * invalidates iterators/references to the element being removed.
994 * The user is also cautioned that this function only erases the
995 * elements, and that if the elements themselves are pointers, the
996 * pointed-to memory is not touched in any way. Managing the pointer
997 * is the user's responsibility.
999 iterator
1000 erase_after(const_iterator __pos, const_iterator __last)
1001 { return iterator(this->_M_erase_after(const_cast<_Node_base*>
1002 (__pos._M_node),
1003 const_cast<_Node_base*>
1004 (__last._M_node))); }
1007 * @brief Swaps data with another %forward_list.
1008 * @param __list A %forward_list of the same element and allocator
1009 * types.
1011 * This exchanges the elements between two lists in constant
1012 * time. Note that the global std::swap() function is
1013 * specialized such that std::swap(l1,l2) will feed to this
1014 * function.
1016 * Whether the allocators are swapped depends on the allocator traits.
1018 void
1019 swap(forward_list& __list) noexcept
1021 std::swap(this->_M_impl._M_head._M_next,
1022 __list._M_impl._M_head._M_next);
1023 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1024 __list._M_get_Node_allocator());
1028 * @brief Resizes the %forward_list to the specified number of
1029 * elements.
1030 * @param __sz Number of elements the %forward_list should contain.
1032 * This function will %resize the %forward_list to the specified
1033 * number of elements. If the number is smaller than the
1034 * %forward_list's current number of elements the %forward_list
1035 * is truncated, otherwise the %forward_list is extended and the
1036 * new elements are default constructed.
1038 void
1039 resize(size_type __sz);
1042 * @brief Resizes the %forward_list to the specified number of
1043 * elements.
1044 * @param __sz Number of elements the %forward_list should contain.
1045 * @param __val Data with which new elements should be populated.
1047 * This function will %resize the %forward_list to the specified
1048 * number of elements. If the number is smaller than the
1049 * %forward_list's current number of elements the %forward_list
1050 * is truncated, otherwise the %forward_list is extended and new
1051 * elements are populated with given data.
1053 void
1054 resize(size_type __sz, const value_type& __val);
1057 * @brief Erases all the elements.
1059 * Note that this function only erases
1060 * the elements, and that if the elements themselves are
1061 * pointers, the pointed-to memory is not touched in any way.
1062 * Managing the pointer is the user's responsibility.
1064 void
1065 clear() noexcept
1066 { this->_M_erase_after(&this->_M_impl._M_head, 0); }
1068 // 23.3.4.6 forward_list operations:
1071 * @brief Insert contents of another %forward_list.
1072 * @param __pos Iterator referencing the element to insert after.
1073 * @param __list Source list.
1075 * The elements of @a list are inserted in constant time after
1076 * the element referenced by @a pos. @a list becomes an empty
1077 * list.
1079 * Requires this != @a x.
1081 void
1082 splice_after(const_iterator __pos, forward_list&& __list) noexcept
1084 if (!__list.empty())
1085 _M_splice_after(__pos, __list.before_begin(), __list.end());
1088 void
1089 splice_after(const_iterator __pos, forward_list& __list) noexcept
1090 { splice_after(__pos, std::move(__list)); }
1093 * @brief Insert element from another %forward_list.
1094 * @param __pos Iterator referencing the element to insert after.
1095 * @param __list Source list.
1096 * @param __i Iterator referencing the element before the element
1097 * to move.
1099 * Removes the element in list @a list referenced by @a i and
1100 * inserts it into the current list after @a pos.
1102 void
1103 splice_after(const_iterator __pos, forward_list&& __list,
1104 const_iterator __i) noexcept;
1106 void
1107 splice_after(const_iterator __pos, forward_list& __list,
1108 const_iterator __i) noexcept
1109 { splice_after(__pos, std::move(__list), __i); }
1112 * @brief Insert range from another %forward_list.
1113 * @param __pos Iterator referencing the element to insert after.
1114 * @param __list Source list.
1115 * @param __before Iterator referencing before the start of range
1116 * in list.
1117 * @param __last Iterator referencing the end of range in list.
1119 * Removes elements in the range (__before,__last) and inserts them
1120 * after @a __pos in constant time.
1122 * Undefined if @a __pos is in (__before,__last).
1123 * @{
1125 void
1126 splice_after(const_iterator __pos, forward_list&&,
1127 const_iterator __before, const_iterator __last) noexcept
1128 { _M_splice_after(__pos, __before, __last); }
1130 void
1131 splice_after(const_iterator __pos, forward_list&,
1132 const_iterator __before, const_iterator __last) noexcept
1133 { _M_splice_after(__pos, __before, __last); }
1134 // @}
1137 * @brief Remove all elements equal to value.
1138 * @param __val The value to remove.
1140 * Removes every element in the list equal to @a __val.
1141 * Remaining elements stay in list order. Note that this
1142 * function only erases the elements, and that if the elements
1143 * themselves are pointers, the pointed-to memory is not
1144 * touched in any way. Managing the pointer is the user's
1145 * responsibility.
1147 void
1148 remove(const _Tp& __val);
1151 * @brief Remove all elements satisfying a predicate.
1152 * @param __pred Unary predicate function or object.
1154 * Removes every element in the list for which the predicate
1155 * returns true. Remaining elements stay in list order. Note
1156 * that this function only erases the elements, and that if the
1157 * elements themselves are pointers, the pointed-to memory is
1158 * not touched in any way. Managing the pointer is the user's
1159 * responsibility.
1161 template<typename _Pred>
1162 void
1163 remove_if(_Pred __pred);
1166 * @brief Remove consecutive duplicate elements.
1168 * For each consecutive set of elements with the same value,
1169 * remove all but the first one. Remaining elements stay in
1170 * list order. Note that this function only erases the
1171 * elements, and that if the elements themselves are pointers,
1172 * the pointed-to memory is not touched in any way. Managing
1173 * the pointer is the user's responsibility.
1175 void
1176 unique()
1177 { unique(std::equal_to<_Tp>()); }
1180 * @brief Remove consecutive elements satisfying a predicate.
1181 * @param __binary_pred Binary predicate function or object.
1183 * For each consecutive set of elements [first,last) that
1184 * satisfy predicate(first,i) where i is an iterator in
1185 * [first,last), remove all but the first one. Remaining
1186 * elements stay in list order. Note that this function only
1187 * erases the elements, and that if the elements themselves are
1188 * pointers, the pointed-to memory is not touched in any way.
1189 * Managing the pointer is the user's responsibility.
1191 template<typename _BinPred>
1192 void
1193 unique(_BinPred __binary_pred);
1196 * @brief Merge sorted lists.
1197 * @param __list Sorted list to merge.
1199 * Assumes that both @a list and this list are sorted according to
1200 * operator<(). Merges elements of @a __list into this list in
1201 * sorted order, leaving @a __list empty when complete. Elements in
1202 * this list precede elements in @a __list that are equal.
1204 void
1205 merge(forward_list&& __list)
1206 { merge(std::move(__list), std::less<_Tp>()); }
1208 void
1209 merge(forward_list& __list)
1210 { merge(std::move(__list)); }
1213 * @brief Merge sorted lists according to comparison function.
1214 * @param __list Sorted list to merge.
1215 * @param __comp Comparison function defining sort order.
1217 * Assumes that both @a __list and this list are sorted according to
1218 * comp. Merges elements of @a __list into this list
1219 * in sorted order, leaving @a __list empty when complete. Elements
1220 * in this list precede elements in @a __list that are equivalent
1221 * according to comp().
1223 template<typename _Comp>
1224 void
1225 merge(forward_list&& __list, _Comp __comp);
1227 template<typename _Comp>
1228 void
1229 merge(forward_list& __list, _Comp __comp)
1230 { merge(std::move(__list), __comp); }
1233 * @brief Sort the elements of the list.
1235 * Sorts the elements of this list in NlogN time. Equivalent
1236 * elements remain in list order.
1238 void
1239 sort()
1240 { sort(std::less<_Tp>()); }
1243 * @brief Sort the forward_list using a comparison function.
1245 * Sorts the elements of this list in NlogN time. Equivalent
1246 * elements remain in list order.
1248 template<typename _Comp>
1249 void
1250 sort(_Comp __comp);
1253 * @brief Reverse the elements in list.
1255 * Reverse the order of elements in the list in linear time.
1257 void
1258 reverse() noexcept
1259 { this->_M_impl._M_head._M_reverse_after(); }
1261 private:
1262 // Called by the range constructor to implement [23.3.4.2]/9
1263 template<typename _InputIterator>
1264 void
1265 _M_range_initialize(_InputIterator __first, _InputIterator __last);
1267 // Called by forward_list(n,v,a), and the range constructor when it
1268 // turns out to be the same thing.
1269 void
1270 _M_fill_initialize(size_type __n, const value_type& __value);
1272 // Called by splice_after and insert_after.
1273 iterator
1274 _M_splice_after(const_iterator __pos, const_iterator __before,
1275 const_iterator __last);
1277 // Called by forward_list(n).
1278 void
1279 _M_default_initialize(size_type __n);
1281 // Called by resize(sz).
1282 void
1283 _M_default_insert_after(const_iterator __pos, size_type __n);
1285 // Called by operator=(forward_list&&)
1286 void
1287 _M_move_assign(forward_list&& __list, std::true_type) noexcept
1289 clear();
1290 this->_M_impl._M_head._M_next = __list._M_impl._M_head._M_next;
1291 __list._M_impl._M_head._M_next = nullptr;
1292 std::__alloc_on_move(this->_M_get_Node_allocator(),
1293 __list._M_get_Node_allocator());
1296 // Called by operator=(forward_list&&)
1297 void
1298 _M_move_assign(forward_list&& __list, std::false_type)
1300 if (__list._M_get_Node_allocator() == this->_M_get_Node_allocator())
1301 _M_move_assign(std::move(__list), std::true_type());
1302 else
1303 // The rvalue's allocator cannot be moved, or is not equal,
1304 // so we need to individually move each element.
1305 this->assign(std::__make_move_if_noexcept_iterator(__list.begin()),
1306 std::__make_move_if_noexcept_iterator(__list.end()));
1309 // Called by assign(_InputIterator, _InputIterator) if _Tp is
1310 // CopyAssignable.
1311 template<typename _InputIterator>
1312 void
1313 _M_assign(_InputIterator __first, _InputIterator __last, true_type)
1315 auto __prev = before_begin();
1316 auto __curr = begin();
1317 auto __end = end();
1318 while (__curr != __end && __first != __last)
1320 *__curr = *__first;
1321 ++__prev;
1322 ++__curr;
1323 ++__first;
1325 if (__first != __last)
1326 insert_after(__prev, __first, __last);
1327 else if (__curr != __end)
1328 erase_after(__prev, __end);
1331 // Called by assign(_InputIterator, _InputIterator) if _Tp is not
1332 // CopyAssignable.
1333 template<typename _InputIterator>
1334 void
1335 _M_assign(_InputIterator __first, _InputIterator __last, false_type)
1337 clear();
1338 insert_after(cbefore_begin(), __first, __last);
1341 // Called by assign(size_type, const _Tp&) if Tp is CopyAssignable
1342 void
1343 _M_assign_n(size_type __n, const _Tp& __val, true_type)
1345 auto __prev = before_begin();
1346 auto __curr = begin();
1347 auto __end = end();
1348 while (__curr != __end && __n > 0)
1350 *__curr = __val;
1351 ++__prev;
1352 ++__curr;
1353 --__n;
1355 if (__n > 0)
1356 insert_after(__prev, __n, __val);
1357 else if (__curr != __end)
1358 erase_after(__prev, __end);
1361 // Called by assign(size_type, const _Tp&) if Tp is non-CopyAssignable
1362 void
1363 _M_assign_n(size_type __n, const _Tp& __val, false_type)
1365 clear();
1366 insert_after(cbefore_begin(), __n, __val);
1370 #if __cpp_deduction_guides >= 201606
1371 template<typename _InputIterator, typename _ValT
1372 = typename iterator_traits<_InputIterator>::value_type,
1373 typename _Allocator = allocator<_ValT>,
1374 typename = _RequireInputIter<_InputIterator>,
1375 typename = _RequireAllocator<_Allocator>>
1376 forward_list(_InputIterator, _InputIterator, _Allocator = _Allocator())
1377 -> forward_list<_ValT, _Allocator>;
1378 #endif
1381 * @brief Forward list equality comparison.
1382 * @param __lx A %forward_list
1383 * @param __ly A %forward_list of the same type as @a __lx.
1384 * @return True iff the elements of the forward lists are equal.
1386 * This is an equivalence relation. It is linear in the number of
1387 * elements of the forward lists. Deques are considered equivalent
1388 * if corresponding elements compare equal.
1390 template<typename _Tp, typename _Alloc>
1391 bool
1392 operator==(const forward_list<_Tp, _Alloc>& __lx,
1393 const forward_list<_Tp, _Alloc>& __ly);
1396 * @brief Forward list ordering relation.
1397 * @param __lx A %forward_list.
1398 * @param __ly A %forward_list of the same type as @a __lx.
1399 * @return True iff @a __lx is lexicographically less than @a __ly.
1401 * This is a total ordering relation. It is linear in the number of
1402 * elements of the forward lists. The elements must be comparable
1403 * with @c <.
1405 * See std::lexicographical_compare() for how the determination is made.
1407 template<typename _Tp, typename _Alloc>
1408 inline bool
1409 operator<(const forward_list<_Tp, _Alloc>& __lx,
1410 const forward_list<_Tp, _Alloc>& __ly)
1411 { return std::lexicographical_compare(__lx.cbegin(), __lx.cend(),
1412 __ly.cbegin(), __ly.cend()); }
1414 /// Based on operator==
1415 template<typename _Tp, typename _Alloc>
1416 inline bool
1417 operator!=(const forward_list<_Tp, _Alloc>& __lx,
1418 const forward_list<_Tp, _Alloc>& __ly)
1419 { return !(__lx == __ly); }
1421 /// Based on operator<
1422 template<typename _Tp, typename _Alloc>
1423 inline bool
1424 operator>(const forward_list<_Tp, _Alloc>& __lx,
1425 const forward_list<_Tp, _Alloc>& __ly)
1426 { return (__ly < __lx); }
1428 /// Based on operator<
1429 template<typename _Tp, typename _Alloc>
1430 inline bool
1431 operator>=(const forward_list<_Tp, _Alloc>& __lx,
1432 const forward_list<_Tp, _Alloc>& __ly)
1433 { return !(__lx < __ly); }
1435 /// Based on operator<
1436 template<typename _Tp, typename _Alloc>
1437 inline bool
1438 operator<=(const forward_list<_Tp, _Alloc>& __lx,
1439 const forward_list<_Tp, _Alloc>& __ly)
1440 { return !(__ly < __lx); }
1442 /// See std::forward_list::swap().
1443 template<typename _Tp, typename _Alloc>
1444 inline void
1445 swap(forward_list<_Tp, _Alloc>& __lx,
1446 forward_list<_Tp, _Alloc>& __ly)
1447 noexcept(noexcept(__lx.swap(__ly)))
1448 { __lx.swap(__ly); }
1450 _GLIBCXX_END_NAMESPACE_CONTAINER
1451 _GLIBCXX_END_NAMESPACE_VERSION
1452 } // namespace std
1454 #endif // _FORWARD_LIST_H