On Tue, Apr 29, 2025 at 10:56 AM Jonathan Wakely <jwak...@redhat.com> wrote:
> Simplify std::vector's use of std::__relocate_a by using 'if constexpr' > even in C++11 and C++14, with diagnostic pragmas to disable warnings. > This allows us to call std::__relocate_a directly, instead of via > _S_relocate and tag distpatching. > > Preserve _S_relocate so that explicit instantiations still get it, but > make it a no-op when _S_use_relocate() is false, so that we don't > instantiate __relocate_a if it isn't needed. > > libstdc++-v3/ChangeLog: > > * include/bits/stl_vector.h (_S_do_relocate): Remove. > (_S_relocate): Remove tag dispatching path. > * include/bits/vector.tcc (reserve, _M_realloc_insert) > (_M_realloc_append, _M_default_append): Add diagnostic pragmas > and use 'if constexpr' in C++11 and C++14. Call > std::__relocate_a directly instead of _S_relocate. > --- > > Tested x86_64-linux. > LGTM, thanks for adding an explanation note for keeping _S_relocate. > > libstdc++-v3/include/bits/stl_vector.h | 26 +++++----------- > libstdc++-v3/include/bits/vector.tcc | 41 +++++++++++++++++--------- > 2 files changed, 34 insertions(+), 33 deletions(-) > > diff --git a/libstdc++-v3/include/bits/stl_vector.h > b/libstdc++-v3/include/bits/stl_vector.h > index aff9d5d9ca5..57680b7bbcf 100644 > --- a/libstdc++-v3/include/bits/stl_vector.h > +++ b/libstdc++-v3/include/bits/stl_vector.h > @@ -518,29 +518,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > return _S_nothrow_relocate(__is_move_insertable<_Tp_alloc_type>{}); > } > > - static pointer > - _S_do_relocate(pointer __first, pointer __last, pointer __result, > - _Tp_alloc_type& __alloc, true_type) noexcept > - { > - return std::__relocate_a(__first, __last, __result, __alloc); > - } > - > - static pointer > - _S_do_relocate(pointer, pointer, pointer __result, > - _Tp_alloc_type&, false_type) noexcept > - { return __result; } > - > static _GLIBCXX20_CONSTEXPR pointer > _S_relocate(pointer __first, pointer __last, pointer __result, > _Tp_alloc_type& __alloc) noexcept > { > -#if __cpp_if_constexpr > - // All callers have already checked _S_use_relocate() so just do > it. > - return std::__relocate_a(__first, __last, __result, __alloc); > -#else > - using __do_it = __bool_constant<_S_use_relocate()>; > - return _S_do_relocate(__first, __last, __result, __alloc, > __do_it{}); > -#endif > +#pragma GCC diagnostic push > +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr > + if constexpr (_S_use_relocate()) > + return std::__relocate_a(__first, __last, __result, __alloc); > + else > + return __result; > +#pragma GCC diagnostic pop > } > #endif // C++11 > > diff --git a/libstdc++-v3/include/bits/vector.tcc > b/libstdc++-v3/include/bits/vector.tcc > index b21e1d3b7a2..e18f01ab0ae 100644 > --- a/libstdc++-v3/include/bits/vector.tcc > +++ b/libstdc++-v3/include/bits/vector.tcc > @@ -61,6 +61,9 @@ namespace std _GLIBCXX_VISIBILITY(default) > _GLIBCXX_BEGIN_NAMESPACE_VERSION > _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > > +#pragma GCC diagnostic push > +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr > + > template<typename _Tp, typename _Alloc> > _GLIBCXX20_CONSTEXPR > void > @@ -74,11 +77,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > const size_type __old_size = size(); > pointer __tmp; > #if __cplusplus >= 201103L > - if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) > + if constexpr (_S_use_relocate()) > { > __tmp = this->_M_allocate(__n); > - _S_relocate(this->_M_impl._M_start, this->_M_impl._M_finish, > - __tmp, _M_get_Tp_allocator()); > + std::__relocate_a(this->_M_impl._M_start, > this->_M_impl._M_finish, > + __tmp, _M_get_Tp_allocator()); > } > else > #endif > @@ -98,6 +101,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; > } > } > +#pragma GCC diagnostic pop > > #if __cplusplus >= 201103L > template<typename _Tp, typename _Alloc> > @@ -444,6 +448,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > #endif > } > > +#pragma GCC diagnostic push > +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr > #if __cplusplus >= 201103L > template<typename _Tp, typename _Alloc> > template<typename... _Args> > @@ -488,14 +494,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > #endif > > #if __cplusplus >= 201103L > - if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) > + if constexpr (_S_use_relocate()) > { > // Relocation cannot throw. > - __new_finish = _S_relocate(__old_start, __position.base(), > - __new_start, _M_get_Tp_allocator()); > + __new_finish = std::__relocate_a(__old_start, > __position.base(), > + __new_start, > + _M_get_Tp_allocator()); > ++__new_finish; > - __new_finish = _S_relocate(__position.base(), __old_finish, > - __new_finish, > _M_get_Tp_allocator()); > + __new_finish = std::__relocate_a(__position.base(), > __old_finish, > + __new_finish, > + _M_get_Tp_allocator()); > } > else > #endif > @@ -593,11 +601,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > #endif > > #if __cplusplus >= 201103L > - if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) > + if constexpr (_S_use_relocate()) > { > // Relocation cannot throw. > - __new_finish = _S_relocate(__old_start, __old_finish, > - __new_start, _M_get_Tp_allocator()); > + __new_finish = std::__relocate_a(__old_start, __old_finish, > + __new_start, > + _M_get_Tp_allocator()); > ++__new_finish; > } > else > @@ -645,6 +654,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > this->_M_impl._M_finish = __new_finish; > this->_M_impl._M_end_of_storage = __new_start + __len; > } > +#pragma GCC diagnostic pop > > template<typename _Tp, typename _Alloc> > _GLIBCXX20_CONSTEXPR > @@ -751,6 +761,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > } > > #if __cplusplus >= 201103L > +#pragma GCC diagnostic push > +#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr > template<typename _Tp, typename _Alloc> > _GLIBCXX20_CONSTEXPR > void > @@ -794,10 +806,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > std::__uninitialized_default_n_a(__new_start + __size, __n, > _M_get_Tp_allocator()); > > - if _GLIBCXX17_CONSTEXPR (_S_use_relocate()) > + if constexpr (_S_use_relocate()) > { > - _S_relocate(__old_start, __old_finish, > - __new_start, _M_get_Tp_allocator()); > + std::__relocate_a(__old_start, __old_finish, > + __new_start, _M_get_Tp_allocator()); > } > else > { > @@ -842,6 +854,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > } > } > } > +#pragma GCC diagnostic pop > > template<typename _Tp, typename _Alloc> > _GLIBCXX20_CONSTEXPR > -- > 2.49.0 > >