[gcc r10-9981] libstdc++: Constrain std::as_writable_bytes [PR101411]

Jonathan Wakely redi@gcc.gnu.org
Wed Jul 14 16:04:54 GMT 2021


https://gcc.gnu.org/g:a34c0973c994d750fb1231da7af96038417b7fe3

commit r10-9981-ga34c0973c994d750fb1231da7af96038417b7fe3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Jul 12 16:09:34 2021 +0100

    libstdc++: Constrain std::as_writable_bytes [PR101411]
    
    The std::as_writable_bytes function should be constrained to only accept
    writable spans. Currently it can be called but then gives an error in
    the function body.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/101411
            * include/std/span (as_writable_bytes): Add requires-clause.
            * testsuite/23_containers/span/101411.cc: New test.
    
    (cherry picked from commit 9d4393af9d2b37b78eb5b1f84f5d4da3a6f7fba6)

Diff:
---
 libstdc++-v3/include/std/span                       |  1 +
 libstdc++-v3/testsuite/23_containers/span/101411.cc | 15 +++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 1cdc0589ddb..9c9a4073507 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -430,6 +430,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
   template<typename _Type, size_t _Extent>
+    requires (!is_const_v<_Type>)
     inline
     span<byte, _Extent == dynamic_extent
        ? dynamic_extent : _Extent * sizeof(_Type)>
diff --git a/libstdc++-v3/testsuite/23_containers/span/101411.cc b/libstdc++-v3/testsuite/23_containers/span/101411.cc
new file mode 100644
index 00000000000..deae73689da
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/span/101411.cc
@@ -0,0 +1,15 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { xfail c++2a } }
+#include <span>
+
+// PR libstdc++/101411
+
+void f(std::span<const int> s)
+{
+  std::as_writable_bytes(s); // { dg-error "unsatisfied constraints" }
+}
+
+void f1(std::span<const int, 1> s)
+{
+  std::as_writable_bytes(s); // { dg-error "unsatisfied constraints" }
+}


More information about the Libstdc++-cvs mailing list