Skip to content

Commit a72ab9c

Browse files
H-G-HristovZingam
andauthored
[libc++][span] P2821R5: span.at() (#74994)
- Implements: [P2821R5: span.at()](https://wg21.link/P2821R5) (https://eel.is/c++draft/views.contiguous#views.span) - Cleaned up `span.operator[]` test --------- Co-authored-by: Zingam <zingam@outlook.com>
1 parent 4daea50 commit a72ab9c

File tree

17 files changed

+280
-2
lines changed

17 files changed

+280
-2
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ Status
436436
--------------------------------------------------- -----------------
437437
``__cpp_lib_smart_ptr_owner_equality`` *unimplemented*
438438
--------------------------------------------------- -----------------
439+
``__cpp_lib_span_at`` ``202311L``
440+
--------------------------------------------------- -----------------
439441
``__cpp_lib_span_initializer_list`` *unimplemented*
440442
--------------------------------------------------- -----------------
441443
``__cpp_lib_sstream_from_string_view`` *unimplemented*

libcxx/docs/ReleaseNotes/18.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Implemented Papers
5757
- P2871R3 - Remove Deprecated Unicode Conversion Facets from C++26
5858
- P2870R3 - Remove basic_string::reserve()
5959
- P2909R4 - Fix formatting of code units as integers (Dude, where’s my ``char``?)
60+
- P2821R5 - span.at()
6061
- P0521R0 - Proposed Resolution for CA 14 (shared_ptr use_count/unique)
6162

6263

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"`P2909R4 <https://wg21.link/P2909R4>`__","LWG","Fix formatting of code units as integers (Dude, where’s my ``char``?)","Kona November 2023","|Complete|","18.0","|format| |DR|"
3636
"`P0952R2 <https://wg21.link/P0952R2>`__","LWG","A new specification for ``std::generate_canonical``","Kona November 2023","","",""
3737
"`P2447R6 <https://wg21.link/P2447R6>`__","LWG","``std::span`` over an initializer list","Kona November 2023","","",""
38-
"`P2821R5 <https://wg21.link/P2821R5>`__","LWG","``span.at()``","Kona November 2023","","",""
38+
"`P2821R5 <https://wg21.link/P2821R5>`__","LWG","``span.at()``","Kona November 2023","|Complete|","18.0",""
3939
"`P2868R3 <https://wg21.link/P2868R3>`__","LWG","Remove Deprecated ``std::allocator`` Typedef From C++26","Kona November 2023","","",""
4040
"`P2870R3 <https://wg21.link/P2870R3>`__","LWG","Remove ``basic_string::reserve()`` From C++26","Kona November 2023","|Complete|","18.0",""
4141
"`P2871R3 <https://wg21.link/P2871R3>`__","LWG","Remove Deprecated Unicode Conversion Facets from C++26","Kona November 2023","|Complete|","18.0",""

libcxx/include/span

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public:
9292
9393
// [span.elem], span element access
9494
constexpr reference operator[](size_type idx) const;
95+
constexpr reference at(size_type idx) const; // since C++26
9596
constexpr reference front() const;
9697
constexpr reference back() const;
9798
constexpr pointer data() const noexcept;
@@ -146,6 +147,7 @@ template<class R>
146147
#include <__utility/forward.h>
147148
#include <array> // for array
148149
#include <cstddef> // for byte
150+
#include <stdexcept>
149151
#include <version>
150152

151153
// standard-mandated includes
@@ -321,6 +323,14 @@ public:
321323
return __data_[__idx];
322324
}
323325

326+
# if _LIBCPP_STD_VER >= 26
327+
_LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
328+
if (__index >= size())
329+
std::__throw_out_of_range("span");
330+
return __data_[__index];
331+
}
332+
# endif
333+
324334
_LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
325335
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T, N>::front() on empty span");
326336
return __data_[0];
@@ -469,6 +479,14 @@ public:
469479
return __data_[__idx];
470480
}
471481

482+
# if _LIBCPP_STD_VER >= 26
483+
_LIBCPP_HIDE_FROM_ABI constexpr reference at(size_type __index) const {
484+
if (__index >= size())
485+
std::__throw_out_of_range("span");
486+
return __data_[__index];
487+
}
488+
# endif
489+
472490
_LIBCPP_HIDE_FROM_ABI constexpr reference front() const noexcept {
473491
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "span<T>::front() on empty span");
474492
return __data_[0];

libcxx/include/version

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ __cpp_lib_smart_ptr_for_overwrite 202002L <memory>
201201
__cpp_lib_smart_ptr_owner_equality 202306L <memory>
202202
__cpp_lib_source_location 201907L <source_location>
203203
__cpp_lib_span 202002L <span>
204+
__cpp_lib_span_at 202311L <span>
204205
__cpp_lib_span_initializer_list 202311L <span>
205206
__cpp_lib_spanstream 202106L <spanstream>
206207
__cpp_lib_ssize 201902L <iterator>
@@ -505,6 +506,7 @@ __cpp_lib_within_lifetime 202306L <type_traits>
505506
// # define __cpp_lib_rcu 202306L
506507
// # define __cpp_lib_saturation_arithmetic 202311L
507508
// # define __cpp_lib_smart_ptr_owner_equality 202306L
509+
# define __cpp_lib_span_at 202311L
508510
// # define __cpp_lib_span_initializer_list 202311L
509511
// # define __cpp_lib_sstream_from_string_view 202306L
510512
// # define __cpp_lib_submdspan 202306L

libcxx/test/libcxx/transitive_includes/cxx03.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ span functional
742742
span initializer_list
743743
span iterator
744744
span limits
745+
span stdexcept
745746
span type_traits
746747
span version
747748
sstream cstddef

libcxx/test/libcxx/transitive_includes/cxx11.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ span functional
748748
span initializer_list
749749
span iterator
750750
span limits
751+
span stdexcept
751752
span type_traits
752753
span version
753754
sstream cstddef

libcxx/test/libcxx/transitive_includes/cxx14.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ span functional
750750
span initializer_list
751751
span iterator
752752
span limits
753+
span stdexcept
753754
span type_traits
754755
span version
755756
sstream cstddef

libcxx/test/libcxx/transitive_includes/cxx17.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ span functional
750750
span initializer_list
751751
span iterator
752752
span limits
753+
span stdexcept
753754
span type_traits
754755
span version
755756
sstream cstddef

libcxx/test/libcxx/transitive_includes/cxx20.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ span functional
755755
span initializer_list
756756
span iterator
757757
span limits
758+
span stdexcept
758759
span type_traits
759760
span version
760761
sstream cstddef

0 commit comments

Comments
 (0)