[libcxx-commits] [PATCH] D112665: [libc++] Ensure valid view for view_interface template parameter

Joe Loser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 27 14:56:06 PDT 2021


jloser created this revision.
jloser added reviewers: ldionne, cjdb, Quuxplusone, Mordante.
jloser requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Some types that inherit from `view_interface` do not meet the
preconditions. This came up during discussion
in https://reviews.llvm.org/D112631. Currently, the behavior is IFNDR,
but the preconditions can be easily checked, so let's do so.

In particular, we know each public member function calls the
`__derived()` private function, so we can do the check there. We
intentionally do it as a `static_assert` instead of a `requires` clause
to avoid hard erroring in some cases. These cases include checking a
particular concept that delegates to a type trait that hard errors when
passed an incomplete type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112665

Files:
  libcxx/include/__ranges/view_interface.h


Index: libcxx/include/__ranges/view_interface.h
===================================================================
--- libcxx/include/__ranges/view_interface.h
+++ libcxx/include/__ranges/view_interface.h
@@ -38,16 +38,29 @@
 template<class _Tp>
 void __implicitly_convert_to(type_identity_t<_Tp>) noexcept;
 
+template<class _Derived>
+  requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
+class view_interface;
+
+template<class _Tp>
+concept __valid_view = requires {
+  sizeof(_Tp);
+  derived_from<_Tp, view_interface<_Tp>>;
+  view<_Tp>;
+};
+
 template<class _Derived>
   requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
 class view_interface : public view_base {
   _LIBCPP_HIDE_FROM_ABI
   constexpr _Derived& __derived() noexcept {
+    static_assert(__valid_view<_Derived>, "The derived type must model derived_from<view_interface<D>> and view");
     return static_cast<_Derived&>(*this);
   }
 
   _LIBCPP_HIDE_FROM_ABI
   constexpr _Derived const& __derived() const noexcept {
+    static_assert(__valid_view<_Derived>, "The derived type must model derived_from<view_interface<D>> and view");
     return static_cast<_Derived const&>(*this);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112665.382806.patch
Type: text/x-patch
Size: 1212 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211027/2ca7b581/attachment.bin>


More information about the libcxx-commits mailing list