]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Simplify std::vector::vector(from_range_t, const Alloc&)
authorJonathan Wakely <jwakely@redhat.com>
Thu, 20 Mar 2025 09:52:35 +0000 (09:52 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 21 Mar 2025 10:28:06 +0000 (10:28 +0000)
Tomasz suggested replacing this constructor with just append_range(rg),
after using a delegating constructor so that the destructor will run if
append_range exits via an exception.

This is slightly less simple than his suggestion, because I want to
avoid the overhead of reserve's slow path and the ASan annotations.
Neither of those is needed for this constructor, because we have no
existing storage to reallocate and no unused capacity to tell ASan
about.

libstdc++-v3/ChangeLog:

* include/bits/stl_vector.h (vector(from_range_t, Alloc)): Use
delegating constructor instead of RAII guards. Use append_range
for unsized input range case.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/stl_vector.h

index 9c75f64b6ef828701f4975dda1b5615870823413..09fd53696d1d8985df8e60f8ad83d7b604745729 100644 (file)
@@ -758,7 +758,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       template<__detail::__container_compatible_range<_Tp> _Rg>
        constexpr
        vector(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
-       : _Base(__a)
+       : vector(__a)
        {
          if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
            {
@@ -766,28 +766,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              pointer __start =
                this->_M_allocate(_S_check_init_len(__n,
                                                    _M_get_Tp_allocator()));
-             _Guard_alloc __guard(__start, __n, *this);
              this->_M_impl._M_finish = this->_M_impl._M_start = __start;
              this->_M_impl._M_end_of_storage = __start + __n;
              _Base::_M_append_range(__rg);
-             (void) __guard._M_release();
            }
          else
-           {
-             // If an exception is thrown ~_Base() will deallocate storage,
-             // but will not destroy elements. This RAII type destroys them.
-             struct _Clear
-             {
-               constexpr ~_Clear() { if (_M_this) _M_this->clear(); }
-               vector* _M_this;
-             } __guard{this};
-
-             auto __first = ranges::begin(__rg);
-             const auto __last = ranges::end(__rg);
-             for (; __first != __last; ++__first)
-               emplace_back(*__first);
-             __guard._M_this = nullptr;
-           }
+           append_range(std::move(__rg));
        }
 #endif