As PR c++/117966 shows, the Debug Mode checks cause a compilation error
for a global constexpr std::span. Those checks are redundant when
constructing from an array or a range, because we already know we have a
valid range and we know its size. Instead of delegating to the
std::span(contiguous_iterator, contiguous_iterator) constructor, just
initialize the data members directly.

libstdc++-v3/ChangeLog:

        PR libstdc++/117966
        * include/std/span (span(T (&)[N])): Do not delegate to
        constructor that preforms redundant checks.
        (span(array<T, N>&), span(const array<T, N>&)): Likewise.
        (span(Range&&), span(const span<T, N>&)): Likewise.
        * testsuite/23_containers/span/117966.cc: New test.
---

Tested x86_64-linux.

This needs to be backported all the way to gcc-12 as well.

 libstdc++-v3/include/std/span                       | 10 +++++-----
 libstdc++-v3/testsuite/23_containers/span/117966.cc | 13 +++++++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/23_containers/span/117966.cc

diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index f1c19b58737..67227348c90 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -190,21 +190,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
        constexpr
        span(type_identity_t<element_type> (&__arr)[_ArrayExtent]) noexcept
-       : span(static_cast<pointer>(__arr), _ArrayExtent)
+       : _M_ptr(__arr), _M_extent(_ArrayExtent)
        { }
 
       template<typename _Tp, size_t _ArrayExtent>
        requires __is_compatible_array<_Tp, _ArrayExtent>::value
        constexpr
        span(array<_Tp, _ArrayExtent>& __arr) noexcept
-       : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
+       : _M_ptr(__arr.data()), _M_extent(_ArrayExtent)
        { }
 
       template<typename _Tp, size_t _ArrayExtent>
        requires __is_compatible_array<const _Tp, _ArrayExtent>::value
        constexpr
        span(const array<_Tp, _ArrayExtent>& __arr) noexcept
-       : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
+       : _M_ptr(__arr.data()), _M_extent(_ArrayExtent)
        { }
 
       template<typename _Range>
@@ -218,7 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        span(_Range&& __range)
        noexcept(noexcept(ranges::data(__range))
                  && noexcept(ranges::size(__range)))
-       : span(ranges::data(__range), ranges::size(__range))
+       : _M_ptr(ranges::data(__range)), _M_extent(ranges::size(__range))
        {
          if constexpr (extent != dynamic_extent)
            {
@@ -236,7 +236,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        constexpr
        explicit(extent != dynamic_extent && _OExtent == dynamic_extent)
        span(const span<_OType, _OExtent>& __s) noexcept
-       : _M_extent(__s.size()), _M_ptr(__s.data())
+       : _M_ptr(__s.data()), _M_extent(__s.size())
        {
          if constexpr (extent != dynamic_extent)
            {
diff --git a/libstdc++-v3/testsuite/23_containers/span/117966.cc 
b/libstdc++-v3/testsuite/23_containers/span/117966.cc
new file mode 100644
index 00000000000..8bbb5ca1e07
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/span/117966.cc
@@ -0,0 +1,13 @@
+// { dg-options "-D_GLIBCXX_DEBUG" }
+// { dg-do compile { target c++20 } }
+
+// Bug 117966
+// constexpr std::span construction fails to compile with D_GLIBCXX_DEBUG
+
+#include <array>
+#include <span>
+
+struct A {
+  constexpr A(std::span<const unsigned char>) {}
+};
+constexpr A val{std::array<unsigned char, 2>{0x11, 0x22}};
-- 
2.47.1

Reply via email to