diff --git a/libstdc++-v3/include/std/valarray b/libstdc++-v3/include/std/valarray index ad3e14ebe52..72eb7a9d56d 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 + [[__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; } /** * @brief Return an iterator pointing to the first element of @@ -1220,9 +1221,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline const _Tp* - begin(const valarray<_Tp>& __va) - { return std::__addressof(__va[0]); } + begin(const valarray<_Tp>& __va) noexcept + { return __va.size() ? std::__addressof(__va[0]) : nullptr; } /** * @brief Return an iterator pointing to one past the last element of @@ -1230,9 +1232,15 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline _Tp* - end(valarray<_Tp>& __va) - { return std::__addressof(__va[0]) + __va.size(); } + end(valarray<_Tp>& __va) noexcept + { + if (auto __n = __va.size()) + return std::__addressof(__va[0]) + __n; + else + return nullptr; + } /** * @brief Return an iterator pointing to one past the last element of @@ -1240,9 +1248,15 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal) * @param __va valarray. */ template + [[__nodiscard__]] inline const _Tp* - end(const valarray<_Tp>& __va) - { return std::__addressof(__va[0]) + __va.size(); } + end(const valarray<_Tp>& __va) noexcept + { + if (auto __n = __va.size()) + return std::__addressof(__va[0]) + __n; + else + return nullptr; + } #endif // C++11 /// @} group numeric_arrays diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc b/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc new file mode 100644 index 00000000000..d2e346760dd --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/valarray/103022.cc @@ -0,0 +1,15 @@ +// { dg-options "-D_GLIBCXX_DEBUG" } +// { dg-do compile { target c++11 } } + +#include + +int main() +{ + // PR libstdc++/103022 + std::valarray va; + (void) std::begin(va); + (void) std::end(va); + const auto& cva = va; + (void) std::begin(cva); + (void) std::end(cva); +} diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc index c015d1890b6..c49c2c52f47 100644 --- a/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc +++ b/libstdc++-v3/testsuite/26_numerics/valarray/range_access.cc @@ -1,4 +1,4 @@ -// { dg-do compile { target c++11 } } +// { dg-do run { target c++11 } } // Copyright (C) 2010-2021 Free Software Foundation, Inc. // @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 26.6.10 valarray range access: [valarray.range] +// C++11 26.6.10 valarray range access: [valarray.range] #include @@ -25,9 +25,46 @@ void test01() { std::valarray va{1.0, 2.0, 3.0}; - std::begin(va); - std::end(va); + (void) std::begin(va); + (void) std::end(va); const auto& cva = va; - std::begin(cva); - std::end(cva); + (void) std::begin(cva); + (void) std::end(cva); + + using Iter = decltype(std::begin(va)); + using IterTraits = std::iterator_traits; + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + using CIter = decltype(std::begin(cva)); + using CIterTraits = std::iterator_traits; + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); + static_assert( std::is_same::value, "" ); +#if __cplusplus >= 202002L + static_assert( std::contiguous_iterator ); + static_assert( std::contiguous_iterator ); +#endif +} + +// PR libstdc++/103022 +void +test02() +{ + std::valarray va; + (void) std::begin(va); + (void) std::end(va); + const auto& cva = va; + (void) std::begin(cva); + (void) std::end(cva); +} + +int main() +{ + test01(); + test02(); } diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc index a7ce1332117..46cfd29e5b2 100644 --- a/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc +++ b/libstdc++-v3/testsuite/26_numerics/valarray/range_access2.cc @@ -1,4 +1,4 @@ -// { dg-do compile { target c++14 } } +// { dg-do run { target c++14 } } // Copyright (C) 2015-2021 Free Software Foundation, Inc. // @@ -17,7 +17,7 @@ // with this library; see the file COPYING3. If not see // . -// 26.6.10 valarray range access: [valarray.range] +// C++11 26.6.10 valarray range access: [valarray.range] #include #include @@ -33,3 +33,21 @@ test01() std::cbegin(cva); std::cend(cva); } + +// PR libstdc++/103022 +void +test02() +{ + std::valarray va; + (void) std::cbegin(va); + (void) std::cend(va); + const auto& cva = va; + (void) std::cbegin(cva); + (void) std::cend(cva); +} + +int main() +{ + test01(); + test02(); +}