]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Add begin, end, (const_)iterator members to valarray.
authorTomasz Kamiński <tkaminsk@redhat.com>
Fri, 15 May 2026 11:41:16 +0000 (13:41 +0200)
committerTomasz Kamiński <tkaminsk@redhat.com>
Mon, 25 May 2026 09:37:31 +0000 (11:37 +0200)
This implements additions from sections 4.3, 4.4, 4.5 of P3016R6.

libstdc++-v3/ChangeLog:

* include/bits/version.def (valarray): Define.
* include/bits/version.h: Regenerate.
* include/std/valarray: (__cpp_lib_valarray): Define.
(valarray::begin, valarray::end, valarray::iterator)
(valarray::const_iterator) [__glibcxx_valarray >= 202511L]: Define.
(std::begin(valarray<_Tp>&), std::begin(const valarray<_Tp>&))
(std::end(valarray<_Tp>&), std::end(const valarray<_Tp>&)): Define
only if __glibcxx_valarray < 202511L (i.e. not defined).
* include/bits/range_access.h (std::valarray)
(std::begin(valarray<_Tp>&), std::begin(const valarray<_Tp>&))
(std::end(valarray<_Tp>&), std::end(const valarray<_Tp>&)): Forward
declare only if __glibcxx_valarray < 202511L (i.e. not defined).
* testsuite/26_numerics/valarray/range_access3.cc: New test.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
Reviewed-by: Nathan Myers <ncm@cantrip.org>
Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
libstdc++-v3/include/bits/range_access.h
libstdc++-v3/include/bits/version.def
libstdc++-v3/include/bits/version.h
libstdc++-v3/include/std/valarray
libstdc++-v3/testsuite/26_numerics/valarray/range_access3.cc [new file with mode: 0644]

index b89129f0233b7cc424984e8ab107850b377ddf2c..01f790868522829c497b2c30d8762574d24da74d 100644 (file)
@@ -119,12 +119,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if __cplusplus >= 201402L
 
+#if __glibcxx_valarray < 202511L
   template<typename _Tp> class valarray;
   // These overloads must be declared for cbegin and cend to use them.
   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;
+#endif
 
   /**
    *  @brief  Return an iterator pointing to the first element of
index 14337b5b6b6d1010591493d868d5b090f6444394..2f32a8bda98f08fc0d4c858aac984d348d13f81e 100644 (file)
@@ -2418,6 +2418,14 @@ ftms = {
   };
 };
 
+ftms = {
+  name = valarray;
+  values = {
+    v = 202511;
+    cxxmin = 26; 
+  };
+};
+
 // Standard test specifications.
 stds[97] = ">= 199711L";
 stds[03] = ">= 199711L";
index 9402f25df375cb39066e3e43b5aa93e0d8f44521..517b9020ec6533bc538c3eff6babbaefb033efe1 100644 (file)
 #endif /* !defined(__cpp_lib_initializer_list) */
 #undef __glibcxx_want_initializer_list
 
+#if !defined(__cpp_lib_valarray)
+# if (__cplusplus >  202302L)
+#  define __glibcxx_valarray 202511L
+#  if defined(__glibcxx_want_all) || defined(__glibcxx_want_valarray)
+#   define __cpp_lib_valarray 202511L
+#  endif
+# endif
+#endif /* !defined(__cpp_lib_valarray) */
+#undef __glibcxx_want_valarray
+
 #undef __glibcxx_want_all
index abb158d22c35672e0ba045de18827cafef119cf4..5a1f5c0ac5e851c9d862c9f1e35536ace74600ef 100644 (file)
@@ -46,6 +46,9 @@
 #include <bits/range_access.h>
 #endif
 
+#define __glibcxx_want_valarray
+#include <bits/version.h>
+
 namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -139,6 +142,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        };
     public:
       typedef _Tp value_type;
+#if __glibcxx_valarray >= 202511L
+      typedef _Tp* iterator;
+      typedef const _Tp* const_iterator;
+#endif
 
        // _lib.valarray.cons_ construct/destroy:
       ///  Construct an empty array.
@@ -572,6 +579,36 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        */
       void resize(size_t __size, _Tp __c = _Tp());
 
+#if __glibcxx_valarray >= 202511L
+      /**
+        *  @brief  Return an iterator pointing to the first element of
+        *          the valarray.
+        */
+      [[nodiscard]]
+      iterator
+      begin() noexcept
+      { return _M_data; }
+
+      [[nodiscard]]
+      const_iterator
+      begin() const noexcept
+      { return _M_data; }
+
+      /**
+        *  @brief  Return an iterator pointing to one past the last element of
+        *          the valarray.
+        */
+      [[nodiscard]]
+      iterator
+      end() noexcept
+      { return _M_data + _M_size; }
+
+      [[nodiscard]]
+      const_iterator
+      end() const noexcept
+      { return _M_data + _M_size; }
+#endif
+
     private:
       size_t _M_size;
       _Tp* __restrict__ _M_data;
@@ -1218,7 +1255,7 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
 #undef _DEFINE_BINARY_OPERATOR
   /// @endcond
 
-#if __cplusplus >= 201103L
+#if (__cplusplus >= 201103L) && (__glibcxx_valarray < 202511L)
   /**
    *  @brief  Return an iterator pointing to the first element of
    *          the valarray.
@@ -1272,7 +1309,7 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
       else
        return nullptr;
     }
-#endif // C++11
+#endif // C++11 to C++23
 
   /// @} group numeric_arrays
 
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/range_access3.cc b/libstdc++-v3/testsuite/26_numerics/valarray/range_access3.cc
new file mode 100644 (file)
index 0000000..09ee7dc
--- /dev/null
@@ -0,0 +1,47 @@
+// { dg-do run { target c++26 } }
+
+#include <valarray>
+#include <testsuite_hooks.h>
+
+#ifndef __cpp_lib_valarray
+# error "Feature-test macro for text_encoding missing in <initializer_list>"
+#elif __cpp_lib_valarray != 202511L
+# error "Feature-test macro for text_encoding has wrong value in <initializer_list>"
+#endif
+
+void
+test01()
+{
+  std::valarray<double> va(3);
+  va[0] = 1.0; va[1] = 2.0; va[2] = 3.0;
+
+  typename std::valarray<double>::iterator it = va.begin();
+  VERIFY( it != va.end() );
+  VERIFY( *it++ == 1.0 );
+  VERIFY( *it++ == 2.0 );
+  VERIFY( *it++ == 3.0 );
+  VERIFY( it == va.end() );
+
+  const std::valarray<double>& cva = va;
+  typename std::valarray<double>::const_iterator cit = cva.begin();
+  VERIFY( cit != va.end() );
+  VERIFY( *cit++ == 1.0 );
+  VERIFY( *cit++ == 2.0 );
+  VERIFY( *cit++ == 3.0 );
+  VERIFY( cit == cva.end() );
+}
+
+void
+test02()
+{
+  std::valarray<double> va;
+  VERIFY( va.begin() == va.end() );
+  const std::valarray<double>& cva = va;
+  VERIFY( cva.begin() == cva.end() );
+}
+
+int main()
+{
+  test01();
+  test02();
+}