[libcxx-commits] [libcxx] Suppress a redundant hardening check in basic_string_view::substr (PR #91804)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 23 10:12:43 PDT 2024


================
@@ -674,6 +677,16 @@ public:
 #endif
 
 private:
+  struct __assume_valid {};
+
+  // This is the same as the pointer and length constructor, but without the additional hardening checks. It is intended
+  // for use within the class, when the class invariants already guarantee the resulting object is valid. The compiler
+  // usually cannot eliminate the redundant checks because it does not know class invariants.
+  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI
+  basic_string_view(const _CharT* __s, size_type __len, __assume_valid) _NOEXCEPT
----------------
mordante wrote:

Typically we use these tags as the first argument.
```suggestion
  basic_string_view(__assume_valid, const _CharT* __s, size_type __len) _NOEXCEPT
```

I was considering to adjust the normal constructor to
```
basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
  : basic_string_view(__assume_valid{} const _CharT* __s, size_type __len, ) 
{
// the hardening checks.
}
```
However we backported `string_view` to C++03 which has no delegating constructors.

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


More information about the libcxx-commits mailing list