[gcc r15-8488] libstdc++: Simplify std::vector::vector(from_range_t, const Alloc&)
Jonathan Wakely
redi@gcc.gnu.org
Fri Mar 21 10:28:48 GMT 2025
https://gcc.gnu.org/g:3e1d760bf49d0e9a89dcfa0450a6d1ae5ceb756e
commit r15-8488-g3e1d760bf49d0e9a89dcfa0450a6d1ae5ceb756e
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu Mar 20 09:52:35 2025 +0000
libstdc++: Simplify std::vector::vector(from_range_t, const Alloc&)
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>
Diff:
---
libstdc++-v3/include/bits/stl_vector.h | 20 ++------------------
1 file changed, 2 insertions(+), 18 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_vector.h b/libstdc++-v3/include/bits/stl_vector.h
index 9c75f64b6ef8..09fd53696d1d 100644
--- a/libstdc++-v3/include/bits/stl_vector.h
+++ b/libstdc++-v3/include/bits/stl_vector.h
@@ -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
More information about the Gcc-cvs
mailing list