[libcxx-commits] [PATCH] D70343: Add a `_LIBCPP_HARDEN` define

Chris Palmer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 13 11:25:15 PDT 2020


palmer marked an inline comment as done.
palmer added inline comments.


================
Comment at: libcxx/include/string_view:282
+    const_reference operator[](size_type __pos) const _NOEXCEPT {
+      return _LIBCPP_ASSERT(size() == 0 || __pos < size(), "string_view[] index out of bounds"), __data[__pos];
+    }
----------------
EricWF wrote:
> jdoerrie wrote:
> > Checking that `size() == 0` seems like a mistake, as otherwise you would allow arbitrary indices for an empty string_view. This should just be `_LIBCPP_ASSERT(__pos < size(), "...");` unless I'm missing something obvious.
> > 
> > Given that string_view implies C++17 or newer, you should also be able to use multiple expressions in a constexpr function. IMO the following is easier to read:
> > 
> > ```
> >     _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
> >     const_reference operator[](size_type __pos) const _NOEXCEPT {
> >       _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds");
> >       return __data[__pos];
> >     }
> > ```
> > 
> `string_view` doesn't imply C++17. We support it in all dialects.
So should I switch back to the previous 2-statement form, again? :)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70343/new/

https://reviews.llvm.org/D70343





More information about the libcxx-commits mailing list