Implement resolutions of LWG 2399, 2400 and 2401.
[gcc.git] / libstdc++-v3 / include / bits / vector.tcc
index 5dc3cd41eb3ce0f081091a8598297734b4544c2f..229fa6d6822b83398adde204d1e3ea20c9dc3255 100644 (file)
@@ -1,11 +1,11 @@
 // Vector implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+// Copyright (C) 2001-2014 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 2, or (at your option)
+// 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,
 // 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 COPYING.  If not, write to the Free
-// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
 
-// As a special exception, you may use this file as part of a free software
-// library without restriction.  Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License.  This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
 
 /*
  *
  * purpose.  It is provided "as is" without express or implied warranty.
  */
 
-/** @file vector.tcc
+/** @file bits/vector.tcc
  *  This is an internal header file, included by other library headers.
- *  You should not attempt to use it directly.
+ *  Do not attempt to use it directly. @headername{vector}
  */
 
 #ifndef _VECTOR_TCC
 #define _VECTOR_TCC 1
 
-namespace _GLIBCXX_STD
+namespace std _GLIBCXX_VISIBILITY(default)
 {
+_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
+
   template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
@@ -74,10 +71,10 @@ namespace _GLIBCXX_STD
        {
          const size_type __old_size = size();
          pointer __tmp = _M_allocate_and_copy(__n,
-                                              this->_M_impl._M_start,
-                                              this->_M_impl._M_finish);
+           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
+           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
          std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                       this->get_allocator());
+                       _M_get_Tp_allocator());
          _M_deallocate(this->_M_impl._M_start,
                        this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_start);
@@ -87,43 +84,81 @@ namespace _GLIBCXX_STD
        }
     }
 
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      vector<_Tp, _Alloc>::
+      emplace_back(_Args&&... __args)
+      {
+       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
+         {
+           _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                    std::forward<_Args>(__args)...);
+           ++this->_M_impl._M_finish;
+         }
+       else
+         _M_emplace_back_aux(std::forward<_Args>(__args)...);
+      }
+#endif
+
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
+#if __cplusplus >= 201103L
+    insert(const_iterator __position, const value_type& __x)
+#else
     insert(iterator __position, const value_type& __x)
+#endif
     {
       const size_type __n = __position - begin();
       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
          && __position == end())
        {
-         this->_M_impl.construct(this->_M_impl._M_finish, __x);
+         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x);
          ++this->_M_impl._M_finish;
        }
       else
-        _M_insert_aux(__position, __x);
-      return begin() + __n;
+       {
+#if __cplusplus >= 201103L
+         const auto __pos = begin() + (__position - cbegin());
+         if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
+           {
+             _Tp __x_copy = __x;
+             _M_insert_aux(__pos, std::move(__x_copy));
+           }
+         else
+           _M_insert_aux(__pos, __x);
+#else
+           _M_insert_aux(__position, __x);
+#endif
+       }
+      return iterator(this->_M_impl._M_start + __n);
     }
 
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
-    erase(iterator __position)
+    _M_erase(iterator __position)
     {
       if (__position + 1 != end())
-        std::copy(__position + 1, end(), __position);
+       _GLIBCXX_MOVE3(__position + 1, end(), __position);
       --this->_M_impl._M_finish;
-      this->_M_impl.destroy(this->_M_impl._M_finish);
+      _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
       return __position;
     }
 
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
-    erase(iterator __first, iterator __last)
+    _M_erase(iterator __first, iterator __last)
     {
-      iterator __i(copy(__last, end(), __first));
-      std::_Destroy(__i, end(), this->get_allocator());
-      this->_M_impl._M_finish = this->_M_impl._M_finish - (__last - __first);
+      if (__first != __last)
+       {
+         if (__last != end())
+           _GLIBCXX_MOVE3(__last, end(), __first);
+         _M_erase_at_end(__first.base() + (end() - __last));
+       }
       return __first;
     }
 
@@ -134,13 +169,32 @@ namespace _GLIBCXX_STD
     {
       if (&__x != this)
        {
+#if __cplusplus >= 201103L
+         if (_Alloc_traits::_S_propagate_on_copy_assign())
+           {
+             if (!_Alloc_traits::_S_always_equal()
+                 && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
+               {
+                 // replacement allocator cannot free existing storage
+                 this->clear();
+                 _M_deallocate(this->_M_impl._M_start,
+                               this->_M_impl._M_end_of_storage
+                               - this->_M_impl._M_start);
+                 this->_M_impl._M_start = nullptr;
+                 this->_M_impl._M_finish = nullptr;
+                 this->_M_impl._M_end_of_storage = nullptr;
+               }
+             std::__alloc_on_copy(_M_get_Tp_allocator(),
+                                  __x._M_get_Tp_allocator());
+           }
+#endif
          const size_type __xlen = __x.size();
          if (__xlen > capacity())
            {
              pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(),
                                                   __x.end());
              std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                           this->get_allocator());
+                           _M_get_Tp_allocator());
              _M_deallocate(this->_M_impl._M_start,
                            this->_M_impl._M_end_of_storage
                            - this->_M_impl._M_start);
@@ -149,16 +203,17 @@ namespace _GLIBCXX_STD
            }
          else if (size() >= __xlen)
            {
-             iterator __i(copy(__x.begin(), __x.end(), begin()));
-             std::_Destroy(__i, end(), this->get_allocator());
+             std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
+                           end(), _M_get_Tp_allocator());
            }
          else
            {
-             std::copy(__x.begin(), __x.begin() + size(),
+             std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
                        this->_M_impl._M_start);
-             std::__uninitialized_copy_a(__x.begin() + size(),
-                                         __x.end(), this->_M_impl._M_finish,
-                                         this->get_allocator());
+             std::__uninitialized_copy_a(__x._M_impl._M_start + size(),
+                                         __x._M_impl._M_finish,
+                                         this->_M_impl._M_finish,
+                                         _M_get_Tp_allocator());
            }
          this->_M_impl._M_finish = this->_M_impl._M_start + __xlen;
        }
@@ -172,19 +227,19 @@ namespace _GLIBCXX_STD
     {
       if (__n > capacity())
        {
-         vector __tmp(__n, __val, get_allocator());
-         __tmp.swap(*this);
+         vector __tmp(__n, __val, _M_get_Tp_allocator());
+         __tmp._M_impl._M_swap_data(this->_M_impl);
        }
       else if (__n > size())
        {
          std::fill(begin(), end(), __val);
-         std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                       __n - size(), __val,
-                                       this->get_allocator());
-         this->_M_impl._M_finish += __n - size();
+         this->_M_impl._M_finish =
+           std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                         __n - size(), __val,
+                                         _M_get_Tp_allocator());
        }
       else
-        erase(fill_n(begin(), __n, __val), end());
+        _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
     }
 
   template<typename _Tp, typename _Alloc>
@@ -192,13 +247,14 @@ namespace _GLIBCXX_STD
       void
       vector<_Tp, _Alloc>::
       _M_assign_aux(_InputIterator __first, _InputIterator __last,
-                   input_iterator_tag)
+                   std::input_iterator_tag)
       {
-       iterator __cur(begin());
-       for (; __first != __last && __cur != end(); ++__cur, ++__first)
+       pointer __cur(this->_M_impl._M_start);
+       for (; __first != __last && __cur != this->_M_impl._M_finish;
+            ++__cur, ++__first)
          *__cur = *__first;
        if (__first == __last)
-         erase(__cur, end());
+         _M_erase_at_end(__cur);
        else
          insert(end(), __first, __last);
       }
@@ -208,7 +264,7 @@ namespace _GLIBCXX_STD
       void
       vector<_Tp, _Alloc>::
       _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
-                   forward_iterator_tag)
+                   std::forward_iterator_tag)
       {
        const size_type __len = std::distance(__first, __last);
 
@@ -216,7 +272,7 @@ namespace _GLIBCXX_STD
          {
            pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
            std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                         this->get_allocator());
+                         _M_get_Tp_allocator());
            _M_deallocate(this->_M_impl._M_start,
                          this->_M_impl._M_end_of_storage
                          - this->_M_impl._M_start);
@@ -225,12 +281,7 @@ namespace _GLIBCXX_STD
            this->_M_impl._M_end_of_storage = this->_M_impl._M_finish;
          }
        else if (size() >= __len)
-         {
-           iterator __new_finish(copy(__first, __last,
-                                      this->_M_impl._M_start));
-           std::_Destroy(__new_finish, end(), this->get_allocator());
-           this->_M_impl._M_finish = __new_finish.base();
-         }
+         _M_erase_at_end(std::copy(__first, __last, this->_M_impl._M_start));
        else
          {
            _ForwardIterator __mid = __first;
@@ -239,72 +290,160 @@ namespace _GLIBCXX_STD
            this->_M_impl._M_finish =
              std::__uninitialized_copy_a(__mid, __last,
                                          this->_M_impl._M_finish,
-                                         this->get_allocator());
+                                         _M_get_Tp_allocator());
          }
       }
 
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      typename vector<_Tp, _Alloc>::iterator
+      vector<_Tp, _Alloc>::
+      emplace(const_iterator __position, _Args&&... __args)
+      {
+       const size_type __n = __position - begin();
+       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
+           && __position == end())
+         {
+           _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                    std::forward<_Args>(__args)...);
+           ++this->_M_impl._M_finish;
+         }
+       else
+         _M_insert_aux(begin() + (__position - cbegin()),
+                       std::forward<_Args>(__args)...);
+       return iterator(this->_M_impl._M_start + __n);
+      }
+
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      vector<_Tp, _Alloc>::
+      _M_insert_aux(iterator __position, _Args&&... __args)
+#else
   template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
     _M_insert_aux(iterator __position, const _Tp& __x)
+#endif
     {
       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
        {
-         this->_M_impl.construct(this->_M_impl._M_finish,
-                                 *(this->_M_impl._M_finish - 1));
+         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                  _GLIBCXX_MOVE(*(this->_M_impl._M_finish
+                                                  - 1)));
          ++this->_M_impl._M_finish;
+#if __cplusplus < 201103L
          _Tp __x_copy = __x;
-         std::copy_backward(__position,
-                            iterator(this->_M_impl._M_finish-2),
-                            iterator(this->_M_impl._M_finish-1));
+#endif
+         _GLIBCXX_MOVE_BACKWARD3(__position.base(),
+                                 this->_M_impl._M_finish - 2,
+                                 this->_M_impl._M_finish - 1);
+#if __cplusplus < 201103L
          *__position = __x_copy;
+#else
+         *__position = _Tp(std::forward<_Args>(__args)...);
+#endif
        }
       else
        {
-         const size_type __old_size = size();
-         if (__old_size == this->max_size())
-           __throw_length_error(__N("vector::_M_insert_aux"));
-
-         // When sizeof(value_type) == 1 and __old_size > size_type(-1)/2
-         // __len overflows: if we don't notice and _M_allocate doesn't
-         // throw we crash badly later.
-         size_type __len = __old_size != 0 ? 2 * __old_size : 1;         
-         if (__len < __old_size)
-           __len = this->max_size();
-
-         iterator __new_start(this->_M_allocate(__len));
-         iterator __new_finish(__new_start);
-         try
+         const size_type __len =
+           _M_check_len(size_type(1), "vector::_M_insert_aux");
+         const size_type __elems_before = __position - begin();
+         pointer __new_start(this->_M_allocate(__len));
+         pointer __new_finish(__new_start);
+         __try
            {
-             __new_finish =
-               std::__uninitialized_copy_a(iterator(this->_M_impl._M_start),
-                                           __position,
-                                           __new_start,
-                                           this->get_allocator());
-             this->_M_impl.construct(__new_finish.base(), __x);
+             // The order of the three operations is dictated by the C++0x
+             // case, where the moves could alter a new element belonging
+             // to the existing vector.  This is an issue only for callers
+             // taking the element by const lvalue ref (see 23.1/13).
+             _Alloc_traits::construct(this->_M_impl,
+                                      __new_start + __elems_before,
+#if __cplusplus >= 201103L
+                                      std::forward<_Args>(__args)...);
+#else
+                                      __x);
+#endif
+             __new_finish = pointer();
+
+             __new_finish
+               = std::__uninitialized_move_if_noexcept_a
+               (this->_M_impl._M_start, __position.base(),
+                __new_start, _M_get_Tp_allocator());
+
              ++__new_finish;
-             __new_finish =
-               std::__uninitialized_copy_a(__position,
-                                           iterator(this->_M_impl._M_finish),
-                                           __new_finish,
-                                           this->get_allocator());
+
+             __new_finish
+               = std::__uninitialized_move_if_noexcept_a
+               (__position.base(), this->_M_impl._M_finish,
+                __new_finish, _M_get_Tp_allocator());
            }
-         catch(...)
+          __catch(...)
            {
-             std::_Destroy(__new_start, __new_finish, this->get_allocator());
-             _M_deallocate(__new_start.base(),__len);
+             if (!__new_finish)
+               _Alloc_traits::destroy(this->_M_impl,
+                                      __new_start + __elems_before);
+             else
+               std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
+             _M_deallocate(__new_start, __len);
              __throw_exception_again;
            }
-         std::_Destroy(begin(), end(), this->get_allocator());
+         std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+                       _M_get_Tp_allocator());
          _M_deallocate(this->_M_impl._M_start,
                        this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_start);
-         this->_M_impl._M_start = __new_start.base();
-         this->_M_impl._M_finish = __new_finish.base();
-         this->_M_impl._M_end_of_storage = __new_start.base() + __len;
+         this->_M_impl._M_start = __new_start;
+         this->_M_impl._M_finish = __new_finish;
+         this->_M_impl._M_end_of_storage = __new_start + __len;
        }
     }
 
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      vector<_Tp, _Alloc>::
+      _M_emplace_back_aux(_Args&&... __args)
+      {
+       const size_type __len =
+         _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
+       pointer __new_start(this->_M_allocate(__len));
+       pointer __new_finish(__new_start);
+       __try
+         {
+           _Alloc_traits::construct(this->_M_impl, __new_start + size(),
+                                    std::forward<_Args>(__args)...);
+           __new_finish = pointer();
+
+           __new_finish
+             = std::__uninitialized_move_if_noexcept_a
+             (this->_M_impl._M_start, this->_M_impl._M_finish,
+              __new_start, _M_get_Tp_allocator());
+
+           ++__new_finish;
+         }
+       __catch(...)
+         {
+           if (!__new_finish)
+             _Alloc_traits::destroy(this->_M_impl, __new_start + size());
+           else
+             std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
+           _M_deallocate(__new_start, __len);
+           __throw_exception_again;
+         }
+       std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+                     _M_get_Tp_allocator());
+       _M_deallocate(this->_M_impl._M_start,
+                     this->_M_impl._M_end_of_storage
+                     - this->_M_impl._M_start);
+       this->_M_impl._M_start = __new_start;
+       this->_M_impl._M_finish = __new_finish;
+       this->_M_impl._M_end_of_storage = __new_start + __len;
+      }
+#endif
+
   template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
@@ -317,96 +456,166 @@ namespace _GLIBCXX_STD
            {
              value_type __x_copy = __x;
              const size_type __elems_after = end() - __position;
-             iterator __old_finish(this->_M_impl._M_finish);
+             pointer __old_finish(this->_M_impl._M_finish);
              if (__elems_after > __n)
                {
-                 std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
+                 std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
                                              this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
-                                             this->get_allocator());
+                                             _M_get_Tp_allocator());
                  this->_M_impl._M_finish += __n;
-                 std::copy_backward(__position, __old_finish - __n,
-                                    __old_finish);
-                 std::fill(__position, __position + __n, __x_copy);
+                 _GLIBCXX_MOVE_BACKWARD3(__position.base(),
+                                         __old_finish - __n, __old_finish);
+                 std::fill(__position.base(), __position.base() + __n,
+                           __x_copy);
                }
              else
                {
-                 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                               __n - __elems_after,
-                                               __x_copy,
-                                               this->get_allocator());
-                 this->_M_impl._M_finish += __n - __elems_after;
-                 std::__uninitialized_copy_a(__position, __old_finish,
+                 this->_M_impl._M_finish =
+                   std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                                 __n - __elems_after,
+                                                 __x_copy,
+                                                 _M_get_Tp_allocator());
+                 std::__uninitialized_move_a(__position.base(), __old_finish,
                                              this->_M_impl._M_finish,
-                                             this->get_allocator());
+                                             _M_get_Tp_allocator());
                  this->_M_impl._M_finish += __elems_after;
-                 std::fill(__position, __old_finish, __x_copy);
+                 std::fill(__position.base(), __old_finish, __x_copy);
                }
            }
          else
            {
-             const size_type __old_size = size();
-             if (this->max_size() - __old_size < __n)
-               __throw_length_error(__N("vector::_M_fill_insert"));
-             
-             // See _M_insert_aux above.
-             size_type __len = __old_size + std::max(__old_size, __n);
-             if (__len < __old_size)
-               __len = this->max_size();
-
-             iterator __new_start(this->_M_allocate(__len));
-             iterator __new_finish(__new_start);
-             try
+             const size_type __len =
+               _M_check_len(__n, "vector::_M_fill_insert");
+             const size_type __elems_before = __position - begin();
+             pointer __new_start(this->_M_allocate(__len));
+             pointer __new_finish(__new_start);
+             __try
                {
-                 __new_finish =
-                   std::__uninitialized_copy_a(begin(), __position,
-                                               __new_start,
-                                               this->get_allocator());
-                 std::__uninitialized_fill_n_a(__new_finish, __n, __x,
-                                               this->get_allocator());
+                 // See _M_insert_aux above.
+                 std::__uninitialized_fill_n_a(__new_start + __elems_before,
+                                               __n, __x,
+                                               _M_get_Tp_allocator());
+                 __new_finish = pointer();
+
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (this->_M_impl._M_start, __position.base(),
+                    __new_start, _M_get_Tp_allocator());
+
                  __new_finish += __n;
-                 __new_finish =
-                   std::__uninitialized_copy_a(__position, end(), __new_finish,
-                                               this->get_allocator());
+
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (__position.base(), this->_M_impl._M_finish,
+                    __new_finish, _M_get_Tp_allocator());
                }
-             catch(...)
+             __catch(...)
                {
-                 std::_Destroy(__new_start, __new_finish,
-                               this->get_allocator());
-                 _M_deallocate(__new_start.base(), __len);
+                 if (!__new_finish)
+                   std::_Destroy(__new_start + __elems_before,
+                                 __new_start + __elems_before + __n,
+                                 _M_get_Tp_allocator());
+                 else
+                   std::_Destroy(__new_start, __new_finish,
+                                 _M_get_Tp_allocator());
+                 _M_deallocate(__new_start, __len);
                  __throw_exception_again;
                }
              std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                           this->get_allocator());
+                           _M_get_Tp_allocator());
              _M_deallocate(this->_M_impl._M_start,
                            this->_M_impl._M_end_of_storage
                            - this->_M_impl._M_start);
-             this->_M_impl._M_start = __new_start.base();
-             this->_M_impl._M_finish = __new_finish.base();
-             this->_M_impl._M_end_of_storage = __new_start.base() + __len;
+             this->_M_impl._M_start = __new_start;
+             this->_M_impl._M_finish = __new_finish;
+             this->_M_impl._M_end_of_storage = __new_start + __len;
            }
        }
     }
 
-  template<typename _Tp, typename _Alloc> template<typename _InputIterator>
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
-    _M_range_insert(iterator __pos, _InputIterator __first,
-                   _InputIterator __last, input_iterator_tag)
+    _M_default_append(size_type __n)
     {
-      for (; __first != __last; ++__first)
+      if (__n != 0)
        {
-         __pos = insert(__pos, *__first);
-         ++__pos;
+         if (size_type(this->_M_impl._M_end_of_storage
+                       - this->_M_impl._M_finish) >= __n)
+           {
+             this->_M_impl._M_finish =
+               std::__uninitialized_default_n_a(this->_M_impl._M_finish,
+                                                __n, _M_get_Tp_allocator());
+           }
+         else
+           {
+             const size_type __len =
+               _M_check_len(__n, "vector::_M_default_append");
+             const size_type __old_size = this->size();
+             pointer __new_start(this->_M_allocate(__len));
+             pointer __new_finish(__new_start);
+             __try
+               {
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (this->_M_impl._M_start, this->_M_impl._M_finish,
+                    __new_start, _M_get_Tp_allocator());
+                 __new_finish =
+                   std::__uninitialized_default_n_a(__new_finish, __n,
+                                                    _M_get_Tp_allocator());
+               }
+             __catch(...)
+               {
+                 std::_Destroy(__new_start, __new_finish,
+                               _M_get_Tp_allocator());
+                 _M_deallocate(__new_start, __len);
+                 __throw_exception_again;
+               }
+             std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+                           _M_get_Tp_allocator());
+             _M_deallocate(this->_M_impl._M_start,
+                           this->_M_impl._M_end_of_storage
+                           - this->_M_impl._M_start);
+             this->_M_impl._M_start = __new_start;
+             this->_M_impl._M_finish = __new_finish;
+             this->_M_impl._M_end_of_storage = __new_start + __len;
+           }
        }
     }
 
+  template<typename _Tp, typename _Alloc>
+    bool
+    vector<_Tp, _Alloc>::
+    _M_shrink_to_fit()
+    {
+      if (capacity() == size())
+       return false;
+      return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
+    }
+#endif
+
+  template<typename _Tp, typename _Alloc>
+    template<typename _InputIterator>
+      void
+      vector<_Tp, _Alloc>::
+      _M_range_insert(iterator __pos, _InputIterator __first,
+                     _InputIterator __last, std::input_iterator_tag)
+      {
+       for (; __first != __last; ++__first)
+         {
+           __pos = insert(__pos, *__first);
+           ++__pos;
+         }
+      }
+
   template<typename _Tp, typename _Alloc>
     template<typename _ForwardIterator>
       void
       vector<_Tp, _Alloc>::
       _M_range_insert(iterator __position, _ForwardIterator __first,
-                     _ForwardIterator __last, forward_iterator_tag)
+                     _ForwardIterator __last, std::forward_iterator_tag)
       {
        if (__first != __last)
          {
@@ -415,16 +624,16 @@ namespace _GLIBCXX_STD
                          - this->_M_impl._M_finish) >= __n)
              {
                const size_type __elems_after = end() - __position;
-               iterator __old_finish(this->_M_impl._M_finish);
+               pointer __old_finish(this->_M_impl._M_finish);
                if (__elems_after > __n)
                  {
-                   std::__uninitialized_copy_a(this->_M_impl._M_finish - __n,
+                   std::__uninitialized_move_a(this->_M_impl._M_finish - __n,
                                                this->_M_impl._M_finish,
                                                this->_M_impl._M_finish,
-                                               this->get_allocator());
+                                               _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __n;
-                   std::copy_backward(__position, __old_finish - __n,
-                                      __old_finish);
+                   _GLIBCXX_MOVE_BACKWARD3(__position.base(),
+                                           __old_finish - __n, __old_finish);
                    std::copy(__first, __last, __position);
                  }
                else
@@ -433,62 +642,246 @@ namespace _GLIBCXX_STD
                    std::advance(__mid, __elems_after);
                    std::__uninitialized_copy_a(__mid, __last,
                                                this->_M_impl._M_finish,
-                                               this->get_allocator());
+                                               _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __n - __elems_after;
-                   std::__uninitialized_copy_a(__position, __old_finish,
+                   std::__uninitialized_move_a(__position.base(),
+                                               __old_finish,
                                                this->_M_impl._M_finish,
-                                               this->get_allocator());
+                                               _M_get_Tp_allocator());
                    this->_M_impl._M_finish += __elems_after;
                    std::copy(__first, __mid, __position);
                  }
              }
            else
              {
-               const size_type __old_size = size();
-               if (this->max_size() - __old_size < __n)
-                 __throw_length_error(__N("vector::_M_range_insert")); 
-
-               // See _M_insert_aux above.
-               size_type __len = __old_size + std::max(__old_size, __n);
-               if (__len < __old_size)
-                 __len = this->max_size();
-
-               iterator __new_start(this->_M_allocate(__len));
-               iterator __new_finish(__new_start);
-               try
+               const size_type __len =
+                 _M_check_len(__n, "vector::_M_range_insert");
+               pointer __new_start(this->_M_allocate(__len));
+               pointer __new_finish(__new_start);
+               __try
                  {
-                   __new_finish =
-                     std::__uninitialized_copy_a(iterator(this->_M_impl._M_start),
-                                                 __position,
-                                                 __new_start,
-                                                 this->get_allocator());
-                   __new_finish =
-                     std::__uninitialized_copy_a(__first, __last, __new_finish,
-                                                 this->get_allocator());
-                   __new_finish =
-                     std::__uninitialized_copy_a(__position,
-                                                 iterator(this->_M_impl._M_finish),
-                                                 __new_finish,
-                                                 this->get_allocator());
+                   __new_finish
+                     = std::__uninitialized_move_if_noexcept_a
+                     (this->_M_impl._M_start, __position.base(),
+                      __new_start, _M_get_Tp_allocator());
+                   __new_finish
+                     = std::__uninitialized_copy_a(__first, __last,
+                                                   __new_finish,
+                                                   _M_get_Tp_allocator());
+                   __new_finish
+                     = std::__uninitialized_move_if_noexcept_a
+                     (__position.base(), this->_M_impl._M_finish,
+                      __new_finish, _M_get_Tp_allocator());
                  }
-               catch(...)
+               __catch(...)
                  {
-                   std::_Destroy(__new_start,__new_finish,
-                                 this->get_allocator());
-                   _M_deallocate(__new_start.base(), __len);
+                   std::_Destroy(__new_start, __new_finish,
+                                 _M_get_Tp_allocator());
+                   _M_deallocate(__new_start, __len);
                    __throw_exception_again;
                  }
                std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
-                             this->get_allocator());
+                             _M_get_Tp_allocator());
                _M_deallocate(this->_M_impl._M_start,
                              this->_M_impl._M_end_of_storage
                              - this->_M_impl._M_start);
-               this->_M_impl._M_start = __new_start.base();
-               this->_M_impl._M_finish = __new_finish.base();
-               this->_M_impl._M_end_of_storage = __new_start.base() + __len;
+               this->_M_impl._M_start = __new_start;
+               this->_M_impl._M_finish = __new_finish;
+               this->_M_impl._M_end_of_storage = __new_start + __len;
+             }
+         }
+      }
+
+
+  // vector<bool>
+  template<typename _Alloc>
+    void
+    vector<bool, _Alloc>::
+    _M_reallocate(size_type __n)
+    {
+      _Bit_pointer __q = this->_M_allocate(__n);
+      iterator __start(std::__addressof(*__q), 0);
+      this->_M_impl._M_finish = _M_copy_aligned(begin(), end(), __start);
+      this->_M_deallocate();
+      this->_M_impl._M_start = __start;
+      this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
+    }
+
+  template<typename _Alloc>
+    void
+    vector<bool, _Alloc>::
+    _M_fill_insert(iterator __position, size_type __n, bool __x)
+    {
+      if (__n == 0)
+       return;
+      if (capacity() - size() >= __n)
+       {
+         std::copy_backward(__position, end(),
+                            this->_M_impl._M_finish + difference_type(__n));
+         std::fill(__position, __position + difference_type(__n), __x);
+         this->_M_impl._M_finish += difference_type(__n);
+       }
+      else
+       {
+         const size_type __len = 
+           _M_check_len(__n, "vector<bool>::_M_fill_insert");
+         _Bit_pointer __q = this->_M_allocate(__len);
+         iterator __start(std::__addressof(*__q), 0);
+         iterator __i = _M_copy_aligned(begin(), __position, __start);
+         std::fill(__i, __i + difference_type(__n), __x);
+         this->_M_impl._M_finish = std::copy(__position, end(),
+                                             __i + difference_type(__n));
+         this->_M_deallocate();
+         this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
+         this->_M_impl._M_start = __start;
+       }
+    }
+
+  template<typename _Alloc>
+    template<typename _ForwardIterator>
+      void
+      vector<bool, _Alloc>::
+      _M_insert_range(iterator __position, _ForwardIterator __first, 
+                     _ForwardIterator __last, std::forward_iterator_tag)
+      {
+       if (__first != __last)
+         {
+           size_type __n = std::distance(__first, __last);
+           if (capacity() - size() >= __n)
+             {
+               std::copy_backward(__position, end(),
+                                  this->_M_impl._M_finish
+                                  + difference_type(__n));
+               std::copy(__first, __last, __position);
+               this->_M_impl._M_finish += difference_type(__n);
+             }
+           else
+             {
+               const size_type __len =
+                 _M_check_len(__n, "vector<bool>::_M_insert_range");
+               _Bit_pointer __q = this->_M_allocate(__len);
+               iterator __start(std::__addressof(*__q), 0);
+               iterator __i = _M_copy_aligned(begin(), __position, __start);
+               __i = std::copy(__first, __last, __i);
+               this->_M_impl._M_finish = std::copy(__position, end(), __i);
+               this->_M_deallocate();
+               this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
+               this->_M_impl._M_start = __start;
              }
          }
       }
+
+  template<typename _Alloc>
+    void
+    vector<bool, _Alloc>::
+    _M_insert_aux(iterator __position, bool __x)
+    {
+      if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_addr())
+       {
+         std::copy_backward(__position, this->_M_impl._M_finish, 
+                            this->_M_impl._M_finish + 1);
+         *__position = __x;
+         ++this->_M_impl._M_finish;
+       }
+      else
+       {
+         const size_type __len =
+           _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
+         _Bit_pointer __q = this->_M_allocate(__len);
+         iterator __start(std::__addressof(*__q), 0);
+         iterator __i = _M_copy_aligned(begin(), __position, __start);
+         *__i++ = __x;
+         this->_M_impl._M_finish = std::copy(__position, end(), __i);
+         this->_M_deallocate();
+         this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
+         this->_M_impl._M_start = __start;
+       }
+    }
+
+  template<typename _Alloc>
+    typename vector<bool, _Alloc>::iterator
+    vector<bool, _Alloc>::
+    _M_erase(iterator __position)
+    {
+      if (__position + 1 != end())
+        std::copy(__position + 1, end(), __position);
+      --this->_M_impl._M_finish;
+      return __position;
+    }
+
+  template<typename _Alloc>
+    typename vector<bool, _Alloc>::iterator
+    vector<bool, _Alloc>::
+    _M_erase(iterator __first, iterator __last)
+    {
+      if (__first != __last)
+       _M_erase_at_end(std::copy(__last, end(), __first));
+      return __first;
+    }
+
+#if __cplusplus >= 201103L
+  template<typename _Alloc>
+    bool
+    vector<bool, _Alloc>::
+    _M_shrink_to_fit()
+    {
+      if (capacity() - size() < int(_S_word_bit))
+       return false;
+      __try
+       {
+         _M_reallocate(size());
+         return true;
+       }
+      __catch(...)
+       { return false; }
+    }
+#endif
+
+_GLIBCXX_END_NAMESPACE_CONTAINER
+} // namespace std
+
+#if __cplusplus >= 201103L
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  template<typename _Alloc>
+    size_t
+    hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
+    operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const noexcept
+    {
+      size_t __hash = 0;
+      using _GLIBCXX_STD_C::_S_word_bit;
+      using _GLIBCXX_STD_C::_Bit_type;
+
+      const size_t __words = __b.size() / _S_word_bit;
+      if (__words)
+       {
+         const size_t __clength = __words * sizeof(_Bit_type);
+         __hash = std::_Hash_impl::hash(__b._M_impl._M_start._M_p, __clength);
+       }
+
+      const size_t __extrabits = __b.size() % _S_word_bit;
+      if (__extrabits)
+       {
+         _Bit_type __hiword = *__b._M_impl._M_finish._M_p;
+         __hiword &= ~((~static_cast<_Bit_type>(0)) << __extrabits);
+
+         const size_t __clength
+           = (__extrabits + __CHAR_BIT__ - 1) / __CHAR_BIT__;
+         if (__words)
+           __hash = std::_Hash_impl::hash(&__hiword, __clength, __hash);
+         else
+           __hash = std::_Hash_impl::hash(&__hiword, __clength);
+       }
+
+      return __hash;
+    }
+
+_GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
+#endif // C++11
+
 #endif /* _VECTOR_TCC */