Commit b5c433ce by Jonathan Wakely Committed by Jonathan Wakely

Improve precondition checks for std::span

	* doc/xml/manual/status_cxx2020.xml: Update status for P0122R7 and
	P1024R3. Remove entry for P0920R2.
	* include/std/span  (__cpp_lib_span): Change value.
	(__extent_storage, __extent_storage<dynamic_extent>): Remove default
	constructor.
	(span): Replace __extent_storage base class with data member.
	(span::_S_subspan_extent): New function.
	(span::empty()): Add nodiscard attribute.
	(span::front, span::back, span::operator[]): Check preconditions.
	(span::first, span::last, span::subspan): Add noexcept. Improve
	precondition checks (LWG 3103).
	(get): Remove redundant condition from static_assert.
	(tuple_element<I, span<T, E>>): Fix static_assert message and simplify.
	(as_writable_bytes): Add inline specifier.
	* include/std/version (__cpp_lib_span): Change value.
	* testsuite/23_containers/span/back_neg.cc: Remove stray semi-colon.
	* testsuite/23_containers/span/front_neg.cc: Likewise.
	* testsuite/23_containers/span/index_op_neg.cc: Likewise.
	* testsuite/23_containers/span/last_neg.cc: Improve test.
	* testsuite/23_containers/span/subspan_neg.cc: Likewise.
	* testsuite/23_containers/span/1.cc: New test.
	* testsuite/23_containers/span/2.cc: New test.
	* testsuite/23_containers/span/back_assert_neg.cc: New test.
	* testsuite/23_containers/span/first_2_assert_neg.cc: New test.
	* testsuite/23_containers/span/first_assert_neg.cc: New test.
	* testsuite/23_containers/span/first_neg.cc: New test.
	* testsuite/23_containers/span/front_assert_neg.cc: New test.
	* testsuite/23_containers/span/index_op_assert_neg.cc: New test.
	* testsuite/23_containers/span/last_2_assert_neg.cc: New test.
	* testsuite/23_containers/span/last_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_2_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_3_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_4_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_5_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_6_assert_neg.cc: New test.
	* testsuite/23_containers/span/subspan_assert_neg.cc: New test.

From-SVN: r275411
parent 55e8f926
2019-09-05 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/status_cxx2020.xml: Update status for P0122R7 and
P1024R3. Remove entry for P0920R2.
* include/std/span (__cpp_lib_span): Change value.
(__extent_storage, __extent_storage<dynamic_extent>): Remove default
constructor.
(span): Replace __extent_storage base class with data member.
(span::_S_subspan_extent): New function.
(span::empty()): Add nodiscard attribute.
(span::front, span::back, span::operator[]): Check preconditions.
(span::first, span::last, span::subspan): Add noexcept. Improve
precondition checks (LWG 3103).
(get): Remove redundant condition from static_assert.
(tuple_element<I, span<T, E>>): Fix static_assert message and simplify.
(as_writable_bytes): Add inline specifier.
* include/std/version (__cpp_lib_span): Change value.
* testsuite/23_containers/span/back_neg.cc: Remove stray semi-colon.
* testsuite/23_containers/span/front_neg.cc: Likewise.
* testsuite/23_containers/span/index_op_neg.cc: Likewise.
* testsuite/23_containers/span/last_neg.cc: Improve test.
* testsuite/23_containers/span/subspan_neg.cc: Likewise.
* testsuite/23_containers/span/1.cc: New test.
* testsuite/23_containers/span/2.cc: New test.
* testsuite/23_containers/span/back_assert_neg.cc: New test.
* testsuite/23_containers/span/first_2_assert_neg.cc: New test.
* testsuite/23_containers/span/first_assert_neg.cc: New test.
* testsuite/23_containers/span/first_neg.cc: New test.
* testsuite/23_containers/span/front_assert_neg.cc: New test.
* testsuite/23_containers/span/index_op_assert_neg.cc: New test.
* testsuite/23_containers/span/last_2_assert_neg.cc: New test.
* testsuite/23_containers/span/last_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_2_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_3_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_4_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_5_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_6_assert_neg.cc: New test.
* testsuite/23_containers/span/subspan_assert_neg.cc: New test.
2019-09-05 JeanHeyd Meneide <phdofthehouse@gmail.com> 2019-09-05 JeanHeyd Meneide <phdofthehouse@gmail.com>
* include/Makefile.am: Add <span> header. * include/Makefile.am: Add <span> header.
......
...@@ -1109,11 +1109,11 @@ Feature-testing recommendations for C++</a>. ...@@ -1109,11 +1109,11 @@ Feature-testing recommendations for C++</a>.
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0777r1.pdf" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0777r1.pdf" target="_top">
P0777R1 P0777R1
</a> </a>
</td><td align="center"> 9.1 </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> <code class="code">&lt;span&gt;</code> </td><td align="left"> </td><td align="center"> 9.1 </td><td align="left"> </td></tr><tr><td align="left"> <code class="code">&lt;span&gt;</code> </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0122r7.pdf" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0122r7.pdf" target="_top">
P0122R7 P0122R7
</a> </a>
</td><td align="center"> </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Extending chrono to Calendars and Time Zones </td><td align="left"> </td><td align="center"> 10.1 </td><td align="left"> <code class="code">__cpp_lib_span &gt;= 201803L</code> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Extending chrono to Calendars and Time Zones </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r7.html" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r7.html" target="_top">
P0355R7 P0355R7
</a> </a>
...@@ -1430,17 +1430,13 @@ Feature-testing recommendations for C++</a>. ...@@ -1430,17 +1430,13 @@ Feature-testing recommendations for C++</a>.
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1252r2.pdf" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1252r2.pdf" target="_top">
P1252R2 P1252R2
</a> </a>
</td><td align="center"> </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> </td><td align="center"> </td><td align="left"> </td></tr><tr><td align="left">
Usability Enhancements for <code class="classname">std::span</code> Usability Enhancements for <code class="classname">std::span</code>
</td><td align="left"> </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1024r3.pdf" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1024r3.pdf" target="_top">
P1024R3 P1024R3
</a> </a>
</td><td align="center"> </td><td align="left"> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Precalculated hash values in lookup </td><td align="left"> </td><td align="center"> 10.1 </td><td align="left"> <code class="code">__cpp_lib_span &gt;= 201902L</code> </td></tr><tr><td align="left"> Traits for [Un]bounded Arrays </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0920r2.html" target="_top">
P0920R2
</a>
</td><td align="center"> </td><td align="left"> </td></tr><tr><td align="left"> Traits for [Un]bounded Arrays </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1357r1.pdf" target="_top"> <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1357r1.pdf" target="_top">
P1357R1 P1357R1
</a> </a>
......
...@@ -237,15 +237,14 @@ Feature-testing recommendations for C++</link>. ...@@ -237,15 +237,14 @@ Feature-testing recommendations for C++</link>.
</row> </row>
<row> <row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> <code>&lt;span&gt;</code> </entry> <entry> <code>&lt;span&gt;</code> </entry>
<entry> <entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0122r7.pdf"> <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0122r7.pdf">
P0122R7 P0122R7
</link> </link>
</entry> </entry>
<entry align="center"> </entry> <entry align="center"> 10.1 </entry>
<entry /> <entry> <code>__cpp_lib_span &gt;= 201803L</code> </entry>
</row> </row>
<row> <row>
...@@ -1111,7 +1110,6 @@ Feature-testing recommendations for C++</link>. ...@@ -1111,7 +1110,6 @@ Feature-testing recommendations for C++</link>.
</row> </row>
<row> <row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> <entry>
Usability Enhancements for <classname>std::span</classname> Usability Enhancements for <classname>std::span</classname>
</entry> </entry>
...@@ -1120,20 +1118,8 @@ Feature-testing recommendations for C++</link>. ...@@ -1120,20 +1118,8 @@ Feature-testing recommendations for C++</link>.
P1024R3 P1024R3
</link> </link>
</entry> </entry>
<entry align="center"> </entry> <entry align="center"> 10.1 </entry>
<entry /> <entry> <code>__cpp_lib_span &gt;= 201902L</code> </entry>
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> Precalculated hash values in lookup </entry>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0920r2.html">
P0920R2
</link>
</entry>
<entry align="center"> </entry>
<entry />
</row> </row>
<row> <row>
......
...@@ -49,9 +49,7 @@ namespace std _GLIBCXX_VISIBILITY(default) ...@@ -49,9 +49,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
{ {
_GLIBCXX_BEGIN_NAMESPACE_VERSION _GLIBCXX_BEGIN_NAMESPACE_VERSION
// FIXME: they forgot this feature test macro #define __cpp_lib_span 201902L
// get on someone's back about it in Belfast!!!
#define __cpp_lib_span 201911
inline constexpr size_t dynamic_extent = static_cast<size_t>(-1); inline constexpr size_t dynamic_extent = static_cast<size_t>(-1);
...@@ -79,9 +77,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -79,9 +77,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
public: public:
constexpr constexpr
__extent_storage() noexcept = default;
constexpr
__extent_storage(size_t) noexcept __extent_storage(size_t) noexcept
{ } { }
...@@ -91,14 +86,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -91,14 +86,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}; };
template<> template<>
class __extent_storage<static_cast<size_t>(-1)> class __extent_storage<dynamic_extent>
{ {
public: public:
constexpr constexpr
__extent_storage() noexcept : _M_extent_value(0)
{ };
constexpr
__extent_storage(size_t __extent) noexcept __extent_storage(size_t __extent) noexcept
: _M_extent_value(__extent) : _M_extent_value(__extent)
{ } { }
...@@ -114,8 +105,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -114,8 +105,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} // namespace __detail } // namespace __detail
template<typename _Type, size_t _Extent = dynamic_extent> template<typename _Type, size_t _Extent = dynamic_extent>
class span : private __detail::__extent_storage<_Extent> class span
{ {
template<size_t _Offset, size_t _Count>
static constexpr size_t
_S_subspan_extent()
{
if constexpr (_Count != dynamic_extent)
return _Count;
else if constexpr (extent != dynamic_extent)
return _Extent - _Offset;
else
return dynamic_extent;
}
public: public:
// member types // member types
using value_type = remove_cv_t<_Type>; using value_type = remove_cv_t<_Type>;
...@@ -138,17 +141,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -138,17 +141,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// member constants // member constants
static inline constexpr size_t extent = _Extent; static inline constexpr size_t extent = _Extent;
private:
using __base_t = __detail::__extent_storage<extent>;
public:
// constructors // constructors
template <typename _Dummy = _Type, template <typename _Dummy = _Type,
enable_if_t<is_same_v<_Dummy, _Type> enable_if_t<is_same_v<_Dummy, _Type>
&& (_Extent == dynamic_extent || _Extent == 0)>* = nullptr> && (_Extent == dynamic_extent || _Extent == 0)>* = nullptr>
constexpr constexpr
span() noexcept : __base_t(0), _M_ptr(nullptr) span() noexcept : _M_extent(0), _M_ptr(nullptr)
{ } { }
constexpr constexpr
...@@ -234,7 +233,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -234,7 +233,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr constexpr
span(_ContiguousIterator __first, index_type __count) span(_ContiguousIterator __first, index_type __count)
noexcept(noexcept(::std::__adl_to_address(__first))) noexcept(noexcept(::std::__adl_to_address(__first)))
: __base_t(__count), _M_ptr(::std::__adl_to_address(__first)) : _M_extent(__count), _M_ptr(::std::__adl_to_address(__first))
{ __glibcxx_assert(_Extent == dynamic_extent || __count == _Extent); } { __glibcxx_assert(_Extent == dynamic_extent || __count == _Extent); }
#else #else
...@@ -276,7 +275,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -276,7 +275,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr constexpr
span(pointer __first, index_type __count) noexcept span(pointer __first, index_type __count) noexcept
: __base_t(__count), _M_ptr(static_cast<pointer>(__first)) : _M_extent(__count), _M_ptr(static_cast<pointer>(__first))
{ __glibcxx_assert(_Extent == dynamic_extent || __count == _Extent); } { __glibcxx_assert(_Extent == dynamic_extent || __count == _Extent); }
constexpr constexpr
...@@ -290,41 +289,52 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -290,41 +289,52 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr span& constexpr span&
operator=(const span&) noexcept = default; operator=(const span&) noexcept = default;
// observers: element access // observers
constexpr index_type
size() const noexcept
{ return this->_M_extent._M_extent(); }
constexpr index_type
size_bytes() const noexcept
{ return this->_M_extent._M_extent() * sizeof(element_type); }
[[nodiscard]] constexpr bool
empty() const noexcept
{ return size() == 0; }
// element access
constexpr reference constexpr reference
front() const noexcept front() const noexcept
{ return *this->begin(); } {
static_assert(extent != 0);
__glibcxx_assert(!empty());
return *this->_M_ptr;
}
constexpr reference constexpr reference
back() const noexcept back() const noexcept
{ {
iterator __it = this->end(); static_assert(extent != 0);
--__it; __glibcxx_assert(!empty());
return *__it; return *(this->_M_ptr + (size() - 1));
} }
constexpr reference constexpr reference
operator[](index_type __idx) const noexcept operator[](index_type __idx) const noexcept
{ return *(this->_M_ptr + __idx); } {
static_assert(extent != 0);
__glibcxx_assert(__idx < size());
return *(this->_M_ptr + __idx);
}
constexpr pointer constexpr pointer
data() const noexcept data() const noexcept
{ return this->_M_ptr; } { return this->_M_ptr; }
constexpr index_type // iterator support
size() const noexcept
{ return this->__base_t::_M_extent(); }
constexpr index_type
size_bytes() const noexcept
{ return this->__base_t::_M_extent() * sizeof(element_type); }
constexpr bool
empty() const noexcept
{ return size() == 0; }
// observers: iterator support
constexpr iterator constexpr iterator
begin() const noexcept begin() const noexcept
{ return iterator(this->_M_ptr); } { return iterator(this->_M_ptr); }
...@@ -357,70 +367,85 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -357,70 +367,85 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
crend() const noexcept crend() const noexcept
{ return const_reverse_iterator(this->cbegin()); } { return const_reverse_iterator(this->cbegin()); }
// observers: subranges // subviews
template<size_t _Count> template<size_t _Count>
constexpr span<element_type, _Count> constexpr span<element_type, _Count>
first() const first() const noexcept
{ {
__glibcxx_assert(_Count < size()); if constexpr (_Extent == dynamic_extent)
__glibcxx_assert(_Count <= size());
else
static_assert(_Count <= extent);
return { this->data(), _Count }; return { this->data(), _Count };
} }
constexpr span<element_type, dynamic_extent> constexpr span<element_type, dynamic_extent>
first(index_type __count) const first(index_type __count) const noexcept
{ {
__glibcxx_assert(__count < size()); __glibcxx_assert(__count <= size());
return { this->data(), __count }; return { this->data(), __count };
} }
template<size_t _Count> template<size_t _Count>
constexpr span<element_type, _Count> constexpr span<element_type, _Count>
last() const last() const noexcept
{ {
static_assert(_Count == dynamic_extent || if constexpr (_Extent == dynamic_extent)
_Extent == dynamic_extent || _Count <= _Extent, __glibcxx_assert(_Count <= size());
"Count or Extent are dynamic, " else
"or the Count is less than the static extent"); static_assert(_Count <= extent);
__glibcxx_assert(_Count <= size());
return { this->data() + (this->size() - _Count), _Count }; return { this->data() + (this->size() - _Count), _Count };
} }
constexpr span<element_type, dynamic_extent> constexpr span<element_type, dynamic_extent>
last(index_type __count) const last(index_type __count) const noexcept
{ {
__glibcxx_assert(__count < size()); __glibcxx_assert(__count <= size());
index_type __offset = (this->size() - __count); return { this->data() + (this->size() - __count), __count };
return { this->data() + __offset, __count };
} }
template<size_t _Offset, size_t _Count = dynamic_extent> template<size_t _Offset, size_t _Count = dynamic_extent>
constexpr auto constexpr auto
subspan() const subspan() const noexcept
-> span<element_type, _S_subspan_extent<_Offset, _Count>()>
{ {
static_assert(_Count == dynamic_extent || if constexpr (_Extent == dynamic_extent)
_Extent == dynamic_extent || __glibcxx_assert(_Offset <= size());
(_Offset + _Count) <= _Extent, else
"Count or Extent are dynamic, " static_assert(_Offset <= extent);
"or the Count + Offset is less than the static extent");
constexpr size_t __span_extent = if constexpr (_Count == dynamic_extent)
(_Count != dynamic_extent return { this->data() + _Offset, this->size() - _Offset };
? _Count else
: (_Extent != dynamic_extent ? _Extent - _Offset
: dynamic_extent));
using __span_t = span<element_type, __span_extent>;
if constexpr(_Count != dynamic_extent)
{ {
__glibcxx_assert((_Offset + _Count) < size()); if constexpr (_Extent == dynamic_extent)
{
__glibcxx_assert(_Count <= size());
__glibcxx_assert(_Count <= (size() - _Offset));
}
else
{
static_assert(_Count <= extent);
static_assert(_Count <= (extent - _Offset));
}
return { this->data() + _Offset, _Count };
} }
return __span_t(this->data() + _Offset,
(_Count == dynamic_extent ? this->size() - _Offset : _Count));
} }
constexpr span<element_type, dynamic_extent> constexpr span<element_type, dynamic_extent>
subspan(index_type __offset, index_type __count = dynamic_extent) const subspan(index_type __offset, index_type __count = dynamic_extent) const
noexcept
{ {
return {this->data() + __offset, __glibcxx_assert(__offset <= size());
__count == dynamic_extent ? this->size() - __offset : __count}; if (__count == dynamic_extent)
__count = this->size() - __offset;
else
{
__glibcxx_assert(__count <= size());
__glibcxx_assert(__offset + __count <= size());
}
return {this->data() + __offset, __count};
} }
// observers: range helpers // observers: range helpers
...@@ -434,51 +459,54 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -434,51 +459,54 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return __sp.end(); } { return __sp.end(); }
private: private:
[[no_unique_address]] __detail::__extent_storage<extent> _M_extent;
pointer _M_ptr; pointer _M_ptr;
}; };
// deduction guides // deduction guides
template<typename _Type, size_t _ArrayExtent> template<typename _Type, size_t _ArrayExtent>
span(_Type(&)[_ArrayExtent])->span<_Type, _ArrayExtent>; span(_Type(&)[_ArrayExtent]) -> span<_Type, _ArrayExtent>;
template<typename _Type, size_t _ArrayExtent> template<typename _Type, size_t _ArrayExtent>
span(array<_Type, _ArrayExtent>&)->span<_Type, _ArrayExtent>; span(array<_Type, _ArrayExtent>&) -> span<_Type, _ArrayExtent>;
template<typename _Type, size_t _ArrayExtent> template<typename _Type, size_t _ArrayExtent>
span(const array<_Type, _ArrayExtent>&) span(const array<_Type, _ArrayExtent>&)
->span<const _Type, _ArrayExtent>; -> span<const _Type, _ArrayExtent>;
#if defined(_GLIBCXX_P1394) && _GLIBCXX_P1394 #if defined(_GLIBCXX_P1394) && _GLIBCXX_P1394
template<typename _ContiguousIterator, typename _Sentinel> template<typename _ContiguousIterator, typename _Sentinel>
span(_ContiguousIterator, _Sentinel) span(_ContiguousIterator, _Sentinel)
->span<remove_reference_t< -> span<remove_reference_t<
typename iterator_traits<_ContiguousIterator>::reference>>; typename iterator_traits<_ContiguousIterator>::reference>>;
template<typename _Range> template<typename _Range>
span(_Range &&) span(_Range &&)
->span<remove_reference_t<typename iterator_traits<decltype( -> span<remove_reference_t<typename iterator_traits<
::std::__adl_begin(::std::declval<_Range&>()))>::reference>>; decltype(std::__adl_begin(::std::declval<_Range&>()))>::reference>>;
#else #else
template<typename _Container> template<typename _Container>
span(_Container&)->span<typename _Container::value_type>; span(_Container&) -> span<typename _Container::value_type>;
template<typename _Container> template<typename _Container>
span(const _Container&)->span<const typename _Container::value_type>; span(const _Container&) -> span<const typename _Container::value_type>;
#endif // P1394 #endif // P1394
template<typename _Type, size_t _Extent> template<typename _Type, size_t _Extent>
inline
span<const byte, _Extent == dynamic_extent span<const byte, _Extent == dynamic_extent
? dynamic_extent : _Extent * sizeof(_Type)> ? dynamic_extent : _Extent * sizeof(_Type)>
inline as_bytes(span<_Type, _Extent> __sp) noexcept as_bytes(span<_Type, _Extent> __sp) noexcept
{ {
return {reinterpret_cast<const byte*>(__sp.data()), __sp.size_bytes()}; return {reinterpret_cast<const byte*>(__sp.data()), __sp.size_bytes()};
} }
template<typename _Type, size_t _Extent> template<typename _Type, size_t _Extent>
inline
span<byte, _Extent == dynamic_extent span<byte, _Extent == dynamic_extent
? dynamic_extent : _Extent * sizeof(_Type)> ? dynamic_extent : _Extent * sizeof(_Type)>
as_writable_bytes(span<_Type, _Extent> __sp) noexcept as_writable_bytes(span<_Type, _Extent> __sp) noexcept
...@@ -491,9 +519,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -491,9 +519,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr _Type& constexpr _Type&
get(span<_Type, _Extent> __sp) noexcept get(span<_Type, _Extent> __sp) noexcept
{ {
static_assert(_Extent != dynamic_extent static_assert(_Extent != dynamic_extent && _Index < _Extent,
&& (_Extent > 0) && (_Index < _Extent), "get<I> can only be used with a span of non-dynamic (fixed) extent");
"std::get can only be used with a span of non-dynamic (fixed) extent");
return __sp[_Index]; return __sp[_Index];
} }
...@@ -508,10 +535,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -508,10 +535,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<size_t _Index, typename _Type, size_t _Extent> template<size_t _Index, typename _Type, size_t _Extent>
struct tuple_element<_Index, span<_Type, _Extent>> struct tuple_element<_Index, span<_Type, _Extent>>
{ {
static_assert(_Extent != dynamic_extent, "tuple_size can only " static_assert(_Extent != dynamic_extent, "tuple_element can only "
"be used with a span of non-dynamic (fixed) extent"); "be used with a span of non-dynamic (fixed) extent");
static_assert(_Index < _Extent, "Index is less than Extent"); static_assert(_Index < _Extent, "Index is less than Extent");
using type = typename span<_Type, _Extent>::element_type; using type = _Type;
}; };
_GLIBCXX_END_NAMESPACE_VERSION _GLIBCXX_END_NAMESPACE_VERSION
......
...@@ -166,10 +166,8 @@ ...@@ -166,10 +166,8 @@
#endif #endif
#define __cpp_lib_list_remove_return_type 201806L #define __cpp_lib_list_remove_return_type 201806L
#define __cpp_lib_math_constants 201907L #define __cpp_lib_math_constants 201907L
#define __cpp_lib_span 201902L
#define __cpp_lib_to_array 201907L #define __cpp_lib_to_array 201907L
// FIXME: they forgot this feature test macro
// get on someone's back about it in Belfast!!!
#define __cpp_lib_span 201911
#endif // C++2a #endif // C++2a
#endif // C++17 #endif // C++17
#endif // C++14 #endif // C++14
......
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <span>
#ifndef __cpp_lib_span
# error "Feature-test macro for span missing in <span>"
#elif __cpp_lib_span != 201902L
# error "Feature-test macro for span has wrong value in <span>"
#endif
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <version>
#ifndef __cpp_lib_span
# error "Feature-test macro for span missing in <version>"
#elif __cpp_lib_span != 201902L
# error "Feature-test macro for span has wrong value in <version>"
#endif
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
std::span<int, std::dynamic_extent> s;
s.back();
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <span>
void
test01()
{
std::span<int, 0> s;
s.back(); // { dg-error "here" }
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.first(5);
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.first<5>();
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <span>
void
test01()
{
int a[4];
std::span<int, 4> s(a);
s.first<5>(); // { dg-error "here" }
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
std::span<int, std::dynamic_extent> s;
s.front();
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <span>
void
test01()
{
std::span<int, 0> s;
s.front(); // { dg-error "here" }
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
std::span<int, std::dynamic_extent> s;
s[99];
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
#include <span>
void
test01()
{
std::span<int, 0> s;
s[99]; // { dg-error "here" }
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.last(5);
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.last<5>();
}
...@@ -15,15 +15,16 @@ ...@@ -15,15 +15,16 @@
// with this library; see the file COPYING3. If not see // with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
// { dg-options "-std=c++2a" } // { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } } // { dg-do compile { target c++2a } }
#include <span> #include <span>
int void
main() test01()
{ {
std::span<int, 2> myspan(nullptr, 2); int a[2];
myspan.last<3>(); // { dg-error "here" } std::span<int, 2> s(a);
s.last<3>(); // { dg-error "here" }
} }
// { dg-error "static assertion failed" "" { target *-*-* } 0 } // { dg-error "static assertion failed" "" { target *-*-* } 0 }
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan<2, 5>();
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan<2, 3>();
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan(5, 0);
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan(2, 5);
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan(2, 3);
}
// Copyright (C) 2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++2a" }
// { dg-do run { xfail c++2a } }
#define _GLIBCXX_ASSERTIONS
#include <span>
int main()
{
int a[4];
std::span<int, std::dynamic_extent> s(a);
s.subspan<5, 0>();
}
...@@ -15,15 +15,33 @@ ...@@ -15,15 +15,33 @@
// with this library; see the file COPYING3. If not see // with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
// { dg-options "-std=c++2a" } // { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } } // { dg-do compile { target c++2a } }
#include <span> #include <span>
int void
main() test01()
{ {
std::span<int, 2> myspan(nullptr, 2); int a[4];
myspan.subspan<3, 1>(); // { dg-error "here" } std::span<int, 4> s(a);
s.subspan<5, 0>(); // { dg-error "here" }
} }
void
test02()
{
int a[4];
std::span<int, 4> s(a);
s.subspan<3, 5>(); // { dg-error "here" }
}
void
test03()
{
int a[4];
std::span<int, 4> s(a);
s.subspan<3, 2>(); // { dg-error "here" }
}
// { dg-error "static assertion failed" "" { target *-*-* } 0 } // { dg-error "static assertion failed" "" { target *-*-* } 0 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment