[clang] [clang-tools-extra] [llvm] [libcxx] [libc++][span] P2821R5: span.at() (PR #74994)
Hristo Hristov via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 10 13:25:58 PST 2023
================
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <span>
+
+// constexpr reference at(size_type idx) const; // since C++26
+
+#include <array>
+#include <cassert>
+#include <concepts>
+#include <span>
+#include <stdexcept>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+
+constexpr void testSpanAt(auto span, int idx, int expectedValue) {
+ // non-const
+ {
+ std::same_as<typename decltype(span)::reference> decltype(auto) elem = span.at(idx);
+ assert(elem == expectedValue);
+ }
+
+ // const
+ {
+ std::same_as<typename decltype(span)::reference> decltype(auto) elem = std::as_const(span).at(idx);
+ assert(elem == expectedValue);
+ }
+}
+
+constexpr bool test() {
+ // With static extent
+ {
+ std::array arr{0, 1, 2, 3, 4, 5, 9084};
+ std::span arrSpan{arr};
+
+ assert(std::dynamic_extent != arrSpan.extent);
+
+ testSpanAt(arrSpan, 0, 0);
+ testSpanAt(arrSpan, 1, 1);
+ testSpanAt(arrSpan, 6, 9084);
+ }
+
+ // With dynamic extent
+ {
+ std::vector vec{0, 1, 2, 3, 4, 5, 9084};
+ std::span vecSpan{vec};
+
+ assert(std::dynamic_extent == vecSpan.extent);
+
+ testSpanAt(vecSpan, 0, 0);
+ testSpanAt(vecSpan, 1, 1);
+ testSpanAt(vecSpan, 6, 9084);
+ }
+
+ return true;
+}
+
+void test_exceptions() {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+ // With static extent
+ {
+ std::array arr{1, 2, 3, 4};
+ const std::span arrSpan{arr};
+
+ try {
+ TEST_IGNORE_NODISCARD arrSpan.at(arr.size() + 1);
----------------
H-G-Hristov wrote:
C++ Core Guidelines: [ES.48: Avoid casts](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es48-avoid-casts)
> Never cast to (void) to ignore a [[nodiscard]]return value. If you deliberately want to discard such a result, first think hard about whether that is really a good idea (there is usually a good reason the author of the function or of the return type used [[nodiscard]] in the first place). If you still think it's appropriate and your code reviewer agrees, use std::ignore = to turn off the warning which is simple, portable, and easy to grep.
Alternatives
...
> - Use std::ignore = to ignore [[nodiscard]] values.
Maybe use `std::ignore` instead?
https://github.com/llvm/llvm-project/pull/74994
More information about the llvm-commits
mailing list