[gcc r12-11043] libstdc++: Skip redundant assertions in std::span construction [PR117966]
Jonathan Wakely
redi@gcc.gnu.org
Wed Apr 16 21:11:17 GMT 2025
https://gcc.gnu.org/g:7b0f505650310c11455ee7d34f95008658d3e655
commit r12-11043-g7b0f505650310c11455ee7d34f95008658d3e655
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Dec 9 17:35:24 2024 +0000
libstdc++: Skip redundant assertions in std::span construction [PR117966]
As PR c++/117966 shows, the Debug Mode checks cause a compilation error
for a global constexpr std::span. Those debug 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 performs 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.
(cherry picked from commit e95bda027e0b81922c1bf44770674190bdf787e8)
Diff:
---
libstdc++-v3/include/std/span | 10 +++++-----
libstdc++-v3/testsuite/23_containers/span/117966.cc | 13 +++++++++++++
2 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span
index 251fed91abf3..2b68ac42ba81 100644
--- a/libstdc++-v3/include/std/span
+++ b/libstdc++-v3/include/std/span
@@ -183,21 +183,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>
@@ -211,7 +211,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)
{
@@ -229,7 +229,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 000000000000..8bbb5ca1e079
--- /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}};
More information about the Libstdc++-cvs
mailing list