On Mon, 1 Nov 2021 at 13:27, Jonathan Wakely via Libstdc++ < libstd...@gcc.gnu.org> wrote:
> Tested powerpc64le-linux, pushed to trunk. > > > The std::begin and std::end overloads for std::valarray are defined in > terms of std::addressof(v[0]) which is undefined for an empty valarray. > > libstdc++-v3/ChangeLog: > > PR libstdc++/103022 > * include/std/valarray (begin, end): Do not dereference an empty > valarray. Add noexcept and [[nodiscard]]. > * testsuite/26_numerics/valarray/range_access.cc: Check empty > valarray. Check iterator properties. Run as well as compiling. > * testsuite/26_numerics/valarray/range_access2.cc: Likewise. > * testsuite/26_numerics/valarray/103022.cc: New test. > --- > libstdc++-v3/include/std/valarray | 30 +++++++++--- > .../testsuite/26_numerics/valarray/103022.cc | 15 ++++++ > .../26_numerics/valarray/range_access.cc | 49 ++++++++++++++++--- > .../26_numerics/valarray/range_access2.cc | 22 ++++++++- > 4 files changed, 100 insertions(+), 16 deletions(-) > create mode 100644 libstdc++-v3/testsuite/26_numerics/valarray/103022.cc > > diff --git a/libstdc++-v3/include/std/valarray > b/libstdc++-v3/include/std/valarray > index 5adc94282ee..c6242eb4db9 100644 > --- a/libstdc++-v3/include/std/valarray > +++ b/libstdc++-v3/include/std/valarray > @@ -1210,9 +1210,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) > * @param __va valarray. > */ > template<class _Tp> > + [[__nodiscard__]] > inline _Tp* > - begin(valarray<_Tp>& __va) > - { return std::__addressof(__va[0]); } > + begin(valarray<_Tp>& __va) noexcept > + { return __va.size() ? std::__addressof(__va[0]) : nullptr; } > > I forgot to also add noexcept to the declarations in <bits/range_access.h>. Fixed by the attached patch, committed to trunk.
commit 2b2d97fc545635a0f6aa9c9ee3b017394bc494bf Author: Jonathan Wakely <jwak...@redhat.com> Date: Fri Nov 5 21:42:20 2021 libstdc++: Fix inconsistent noexcept-specific for valarray begin/end These declarations should be noexcept after I added it to the definitions in <valarray>. libstdc++-v3/ChangeLog: * include/bits/range_access.h (begin(valarray), end(valarray)): Add noexcept. diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h index 3dec687dd94..5e4c4727ebf 100644 --- a/libstdc++-v3/include/bits/range_access.h +++ b/libstdc++-v3/include/bits/range_access.h @@ -110,10 +110,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template<typename _Tp> class valarray; // These overloads must be declared for cbegin and cend to use them. - template<typename _Tp> _Tp* begin(valarray<_Tp>&); - template<typename _Tp> const _Tp* begin(const valarray<_Tp>&); - template<typename _Tp> _Tp* end(valarray<_Tp>&); - template<typename _Tp> const _Tp* end(const valarray<_Tp>&); + template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept; + template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept; + template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept; + template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept; /** * @brief Return an iterator pointing to the first element of