From 9b8e2dea783b3e67813b12c7cb3036b5a9892c65 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Tue, 18 Feb 2020 15:51:27 +0000 Subject: libstdc++: P1976R2 Fixed-size span construction from dynamic range This includes fixes for first, last, as_bytes and as_writable_bytes which were missing from the paper. * include/std/span (__cpp_lib_span): Update value. (span(It, size_type), span(It, End)): Make conditionally explicit. Add assertion. (span(R&&), span(const span&)): Likewise and relax constraints. (span::first(), span::last()): Use explicit type in return statement. (as_bytes, as_writable_bytes): Likewise. * include/std/version (__cpp_lib_span): Update value. * testsuite/23_containers/span/1.cc: Check new value. * testsuite/23_containers/span/2.cc: Check new value. * testsuite/23_containers/span/explicit.cc: New test. --- libstdc++-v3/include/std/span | 70 +++++++++++++++++++++++++++++----------- libstdc++-v3/include/std/version | 2 +- 2 files changed, 52 insertions(+), 20 deletions(-) (limited to 'libstdc++-v3/include/std') diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index 21114f1..a8c6979 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -48,7 +48,7 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION -#define __cpp_lib_span 201902L +#define __cpp_lib_span 202002L inline constexpr size_t dynamic_extent = static_cast(-1); @@ -158,23 +158,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template requires __is_compatible_ref>::value - constexpr + constexpr explicit(extent != dynamic_extent) span(_It __first, size_type __count) noexcept : _M_extent(__count), _M_ptr(std::to_address(__first)) - { __glibcxx_assert(_Extent == dynamic_extent || __count == _Extent); } + { + if constexpr (_Extent != dynamic_extent) + { + __glibcxx_assert(__count == _Extent); + } + } template _End> requires __is_compatible_ref>::value && (!is_convertible_v<_End, size_type>) - constexpr + constexpr explicit(extent != dynamic_extent) span(_It __first, _End __last) noexcept(noexcept(__last - __first)) : _M_extent(static_cast(__last - __first)), _M_ptr(std::to_address(__first)) { - if (_Extent != dynamic_extent) - __glibcxx_assert((__last - __first) == _Extent); + if constexpr (_Extent != dynamic_extent) + { + __glibcxx_assert((__last - __first) == _Extent); + } } template @@ -199,30 +206,41 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } template - requires (_Extent == dynamic_extent) - && ranges::contiguous_range<_Range> && ranges::sized_range<_Range> + requires ranges::contiguous_range<_Range> && ranges::sized_range<_Range> && (ranges::safe_range<_Range> || is_const_v) && (!__detail::__is_std_span>::value) && (!__detail::__is_std_array>::value) && (!is_array_v>) && __is_compatible_ref>::value - constexpr + constexpr explicit(extent != dynamic_extent) span(_Range&& __range) noexcept(noexcept(ranges::data(__range)) && noexcept(ranges::size(__range))) : span(ranges::data(__range), ranges::size(__range)) - { } + { + if constexpr (extent != dynamic_extent) + { + __glibcxx_assert(ranges::size(__range) == extent); + } + } constexpr span(const span&) noexcept = default; template - requires (_Extent == dynamic_extent || _Extent == _OExtent) + requires (_Extent == dynamic_extent || _OExtent == dynamic_extent + || _Extent == _OExtent) && (__is_array_convertible<_Type, _OType>::value) constexpr + explicit(extent != dynamic_extent && _OExtent == dynamic_extent) span(const span<_OType, _OExtent>& __s) noexcept : _M_extent(__s.size()), _M_ptr(__s.data()) - { } + { + if constexpr (extent != dynamic_extent) + { + __glibcxx_assert(__s.size() == extent); + } + } ~span() noexcept = default; @@ -317,7 +335,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __glibcxx_assert(_Count <= size()); else static_assert(_Count <= extent); - return { this->data(), _Count }; + using _Sp = span; + return _Sp{ this->data(), _Count }; } constexpr span @@ -335,7 +354,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __glibcxx_assert(_Count <= size()); else static_assert(_Count <= extent); - return { this->data() + (this->size() - _Count), _Count }; + using _Sp = span; + return _Sp{ this->data() + (this->size() - _Count), _Count }; } constexpr span @@ -351,12 +371,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION -> span()> { if constexpr (_Extent == dynamic_extent) - __glibcxx_assert(_Offset <= size()); + { + __glibcxx_assert(_Offset <= size()); + } else static_assert(_Offset <= extent); + using _Sp = span()>; + if constexpr (_Count == dynamic_extent) - return { this->data() + _Offset, this->size() - _Offset }; + return _Sp{ this->data() + _Offset, this->size() - _Offset }; else { if constexpr (_Extent == dynamic_extent) @@ -369,7 +393,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static_assert(_Count <= extent); static_assert(_Count <= (extent - _Offset)); } - return { this->data() + _Offset, _Count }; + return _Sp{ this->data() + _Offset, _Count }; } } @@ -419,7 +443,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ? dynamic_extent : _Extent * sizeof(_Type)> as_bytes(span<_Type, _Extent> __sp) noexcept { - return {reinterpret_cast(__sp.data()), __sp.size_bytes()}; + auto data = reinterpret_cast(__sp.data()); + auto size = __sp.size_bytes(); + constexpr auto extent = _Extent == dynamic_extent + ? dynamic_extent : _Extent * sizeof(_Type); + return span{data, size}; } template @@ -428,7 +456,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ? dynamic_extent : _Extent * sizeof(_Type)> as_writable_bytes(span<_Type, _Extent> __sp) noexcept { - return {reinterpret_cast(__sp.data()), __sp.size_bytes()}; + auto data = reinterpret_cast(__sp.data()); + auto size = __sp.size_bytes(); + constexpr auto extent = _Extent == dynamic_extent + ? dynamic_extent : _Extent * sizeof(_Type); + return span{data, size}; } namespace ranges diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index f9877ef..b36b999 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -193,7 +193,7 @@ #endif #define __cpp_lib_list_remove_return_type 201806L #define __cpp_lib_math_constants 201907L -#define __cpp_lib_span 201902L +#define __cpp_lib_span 202002L #if __cpp_impl_three_way_comparison >= 201907L && __cpp_lib_concepts # define __cpp_lib_three_way_comparison 201711L #endif -- cgit v1.1