[libcxx-commits] [libcxx] [libc++][ranges] P3052R2: `view_interface::at()` (PR #205012)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 27 22:56:16 PDT 2026


================
@@ -159,6 +161,30 @@ class view_interface {
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](range_difference_t<_RARange> __index) const {
     return ranges::begin(__derived())[__index];
   }
+
+#  if _LIBCPP_STD_VER >= 29
+
+  template <random_access_range _RARange = _Derived>
+    requires sized_range<_RARange> // freestanding-deleted
+  [[nodiscard]] constexpr decltype(auto) at(range_difference_t<_RARange> __index) {
+    if (__index < 0 || __index >= ranges::distance(__derived())) {
+      std::__throw_out_of_range(
+          "Precondition `0 <= __index < distance()` not satisfied. `.at(__index)` called with out-of-bounds index.");
----------------
frederick-vs-ja wrote:

Mostly LGTM. But I'm not sure whether we should say "Precondition" here. I personally don't want to say so because in the Standardese violating preconditions means UB.

```suggestion
      std::__throw_out_of_range("The index passed to `view_interface::at()` is out of bound of the view.");
```

BTW, it might be helpful to say the actual index and bound in the exception message, but that would require a lot more mechanisms.

https://github.com/llvm/llvm-project/pull/205012


More information about the libcxx-commits mailing list