From: Jonathan Wakely Date: Tue, 8 Jul 2025 13:56:39 +0000 (+0100) Subject: libstdc++: Do not use list-initialization in std::span members [PR120997] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab3781665da064985d66de0c895cc43588179cb6;p=thirdparty%2Fgcc.git libstdc++: Do not use list-initialization in std::span members [PR120997] As the bug report shows, for span the return statements of the form `return {data(), count};` will use the new C++26 constructor, span(initializer_list). Although the conversions from data() to bool and count to bool are narrowing and should be ill-formed, in system headers the narrowing diagnostics are suppressed. In any case, even if the compiler diagnosed them as ill-formed, we still don't want the initializer_list constructor to be used. We want to use the span(element_type*, size_t) constructor instead. Replace the braced-init-list uses with S(data(), count) where S is the correct return type. We need to make similar changes in the C++26 working draft, which will be taken care of via an LWG issue. libstdc++-v3/ChangeLog: PR libstdc++/120997 * include/std/span (span::first, span::last, span::subspan): Do not use braced-init-list for return statements. * testsuite/23_containers/span/120997.cc: New test. (cherry picked from commit a72d0e1a8bf0770ddf1d8d0ebe589f92a4fab4ef) --- diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index 49ab9109d83e..6f0213e6bf4e 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -376,7 +376,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else static_assert(_Count <= extent); using _Sp = span; - return _Sp{ _SizedPtr{this->data()} }; + return _Sp(_SizedPtr{this->data()}); } [[nodiscard]] @@ -384,7 +384,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION first(size_type __count) const noexcept { __glibcxx_assert(__count <= size()); - return { this->data(), __count }; + return span(this->data(), __count); } template @@ -397,7 +397,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else static_assert(_Count <= extent); using _Sp = span; - return _Sp{ _SizedPtr{this->data() + (this->size() - _Count)} }; + return _Sp(_SizedPtr{this->data() + (this->size() - _Count)}); } [[nodiscard]] @@ -405,7 +405,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION last(size_type __count) const noexcept { __glibcxx_assert(__count <= size()); - return { this->data() + (this->size() - __count), __count }; + return span(this->data() + (this->size() - __count), + __count); } template @@ -424,7 +425,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using _Sp = span()>; if constexpr (_Count == dynamic_extent) - return _Sp{ this->data() + _Offset, this->size() - _Offset }; + return _Sp(this->data() + _Offset, this->size() - _Offset); else { if constexpr (_Extent == dynamic_extent) @@ -437,7 +438,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(_Count <= extent); static_assert(_Count <= (extent - _Offset)); } - return _Sp{ _SizedPtr{this->data() + _Offset} }; + return _Sp(_SizedPtr{this->data() + _Offset}); } } @@ -454,7 +455,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __glibcxx_assert(__count <= size()); __glibcxx_assert(__offset + __count <= size()); } - return {this->data() + __offset, __count}; + return span(this->data() + __offset, __count); } private: diff --git a/libstdc++-v3/testsuite/23_containers/span/120997.cc b/libstdc++-v3/testsuite/23_containers/span/120997.cc new file mode 100644 index 000000000000..fbf194c87388 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/span/120997.cc @@ -0,0 +1,46 @@ +// { dg-do run { target c++26 } } + +#include +#include + +void +test_first() +{ + bool arr[5]; + std::span s(arr); + std::span s2 = s.first(5); + VERIFY( s2.data() == s.data() ); + std::span s3 = s.first<5>(); + VERIFY( s3.data() == s.data() ); +} + +void +test_last() +{ + bool arr[5]; + std::span s(arr); + std::span s2 = s.last(5); + VERIFY( s2.data() == s.data() ); + std::span s3 = s.last<5>(); + VERIFY( s3.data() == s.data() ); +} + +void +test_subspan() +{ + bool arr[5]; + std::span s(arr); + std::span s2 = s.subspan(0, 5); + VERIFY( s2.data() == s.data() ); + std::span s3 = s.subspan<0>(); + VERIFY( s3.data() == s.data() ); + std::span s4 = s.subspan<0, 5>(); + VERIFY( s4.data() == s.data() ); +} + +int main() +{ + test_first(); + test_last(); + test_subspan(); +}