-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[370] - Did you know that C++26 added
span.at
?
- Loading branch information
1 parent
f4fd31f
commit fb693d1
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know that C++26 added `span.at`? | ||
|
||
* https://wg21.link/P2821 | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
std::array<int, 42> storage{}; | ||
const auto span = std::span(storage); | ||
(void)span.at(0); // okay | ||
(void)span.at(storage.size()); // throws | ||
``` | ||
|
||
> https://godbolt.org/z/ezvMvraW1 | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
|
||
* **Can you implement `span_at` which will use `[]` if `fn` is noexcept and `at` otherwise? | ||
|
||
```cpp | ||
struct span_at; // TODO | ||
|
||
int main() { | ||
using namespace boost::ut; | ||
|
||
std::array<int, 42> storage{}; | ||
for (auto i = 0; i < storage.size(); ++i) { storage[i] = i; } | ||
|
||
"span[index]"_test = [&] { | ||
span_at t{storage}; | ||
expect(0_i == t([](auto i) noexcept(true) { return i; }, 0)); | ||
expect(2_i == t([](auto i) noexcept(true) { return i; }, 2)); | ||
expect(41_i == t([](auto i) noexcept(true) { return i; }, 41)); | ||
// expect(not throws([&] { (void)t([](auto i) noexcept(true) { return i; }, 42); })); | ||
}; | ||
|
||
"span.at"_test = [&] { | ||
span_at t{storage}; | ||
expect(0_i == t([](auto i) noexcept(false) { return i; }, 0)); | ||
expect(2_i == t([](auto i) noexcept(false) { return i; }, 2)); | ||
expect(41_i == t([](auto i) noexcept(false) { return i; }, 41)); | ||
expect(throws([&] { (void)t([](auto i) noexcept(false) { return i; }, 42); })); | ||
}; | ||
} | ||
``` | ||
|
||
> https://godbolt.org/z/oTxo9aaz4 | ||
</p></details> | ||
|
||
</p></details><details><summary>Solutions</summary><p> | ||
|
||
```cpp | ||
struct span_at { | ||
constexpr explicit span_at(std::span<const int> span) : span_{span} { } | ||
|
||
[[nodiscard]] constexpr auto operator()(auto&& fn, auto index) { | ||
if constexpr(noexcept(fn(span_[index]))) { | ||
return fn(span_[index]); | ||
} else { | ||
return fn(span_.at(index)); | ||
} | ||
} | ||
|
||
private: | ||
std::span<const int> span_{}; | ||
}; | ||
``` | ||
> https://godbolt.org/z/hcnWjWodM | ||
</p></details> |