Implement resolutions of LWG 2399, 2400 and 2401.
[gcc.git] / libstdc++-v3 / include / bits / vector.tcc
index 8e22d10a26e5a36e04a114cc5ebc5b9c45f18d7c..229fa6d6822b83398adde204d1e3ea20c9dc3255 100644 (file)
@@ -1,6 +1,6 @@
 // Vector implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001-2013 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
@@ -121,14 +121,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       else
        {
 #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(__position._M_const_cast(), std::move(__x_copy));
+             _M_insert_aux(__pos, std::move(__x_copy));
            }
          else
+           _M_insert_aux(__pos, __x);
+#else
+           _M_insert_aux(__position, __x);
 #endif
-           _M_insert_aux(__position._M_const_cast(), __x);
        }
       return iterator(this->_M_impl._M_start + __n);
     }
@@ -225,15 +228,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       if (__n > capacity())
        {
          vector __tmp(__n, __val, _M_get_Tp_allocator());
-         __tmp.swap(*this);
+         __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,
-                                       _M_get_Tp_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
         _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
@@ -307,7 +310,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
            ++this->_M_impl._M_finish;
          }
        else
-         _M_insert_aux(__position._M_const_cast(),
+         _M_insert_aux(begin() + (__position - cbegin()),
                        std::forward<_Args>(__args)...);
        return iterator(this->_M_impl._M_start + __n);
       }
@@ -362,7 +365,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 #else
                                       __x);
 #endif
-             __new_finish = 0;
+             __new_finish = pointer();
 
              __new_finish
                = std::__uninitialized_move_if_noexcept_a
@@ -412,7 +415,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          {
            _Alloc_traits::construct(this->_M_impl, __new_start + size(),
                                     std::forward<_Args>(__args)...);
-           __new_finish = 0;
+           __new_finish = pointer();
 
            __new_finish
              = std::__uninitialized_move_if_noexcept_a
@@ -468,11 +471,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                }
              else
                {
-                 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                               __n - __elems_after,
-                                               __x_copy,
-                                               _M_get_Tp_allocator());
-                 this->_M_impl._M_finish += __n - __elems_after;
+                 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,
                                              _M_get_Tp_allocator());
@@ -493,7 +496,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                  std::__uninitialized_fill_n_a(__new_start + __elems_before,
                                                __n, __x,
                                                _M_get_Tp_allocator());
-                 __new_finish = 0;
+                 __new_finish = pointer();
 
                  __new_finish
                    = std::__uninitialized_move_if_noexcept_a
@@ -542,9 +545,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          if (size_type(this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_finish) >= __n)
            {
-             std::__uninitialized_default_n_a(this->_M_impl._M_finish,
-                                              __n, _M_get_Tp_allocator());
-             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
            {
@@ -559,9 +562,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                    = std::__uninitialized_move_if_noexcept_a
                    (this->_M_impl._M_start, this->_M_impl._M_finish,
                     __new_start, _M_get_Tp_allocator());
-                 std::__uninitialized_default_n_a(__new_finish, __n,
-                                                  _M_get_Tp_allocator());
-                 __new_finish += __n;
+                 __new_finish =
+                   std::__uninitialized_default_n_a(__new_finish, __n,
+                                                    _M_get_Tp_allocator());
                }
              __catch(...)
                {
@@ -696,11 +699,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     vector<bool, _Alloc>::
     _M_reallocate(size_type __n)
     {
-      _Bit_type* __q = this->_M_allocate(__n);
-      this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
-                                               iterator(__q, 0));
+      _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 = iterator(__q, 0);
+      this->_M_impl._M_start = __start;
       this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
     }
 
@@ -722,15 +725,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        {
          const size_type __len = 
            _M_check_len(__n, "vector<bool>::_M_fill_insert");
-         _Bit_type * __q = this->_M_allocate(__len);
-         iterator __i = _M_copy_aligned(begin(), __position,
-                                        iterator(__q, 0));
+         _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 = iterator(__q, 0);
+         this->_M_impl._M_start = __start;
        }
     }
 
@@ -756,14 +759,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              {
                const size_type __len =
                  _M_check_len(__n, "vector<bool>::_M_insert_range");
-               _Bit_type * __q = this->_M_allocate(__len);
-               iterator __i = _M_copy_aligned(begin(), __position,
-                                              iterator(__q, 0));
+               _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 = iterator(__q, 0);
+               this->_M_impl._M_start = __start;
              }
          }
       }
@@ -773,7 +776,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     vector<bool, _Alloc>::
     _M_insert_aux(iterator __position, bool __x)
     {
-      if (this->_M_impl._M_finish._M_p != this->_M_impl._M_end_of_storage)
+      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);
@@ -784,14 +787,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        {
          const size_type __len =
            _M_check_len(size_type(1), "vector<bool>::_M_insert_aux");
-         _Bit_type * __q = this->_M_allocate(__len);
-         iterator __i = _M_copy_aligned(begin(), __position,
-                                        iterator(__q, 0));
+         _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 = iterator(__q, 0);
+         this->_M_impl._M_start = __start;
        }
     }