[libcxx-commits] [libcxx] [libc++][ranges] add static_assert for ranges::to (PR #135802)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 16 03:50:44 PDT 2025


https://github.com/Yuzhiy05 updated https://github.com/llvm/llvm-project/pull/135802

>From 7515cdb203f3c2f1c0734c68de8e6b05d4573655 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Tue, 15 Apr 2025 23:36:08 +0800
Subject: [PATCH 1/3] [libc++][ranges]add static_assert for ranges::to

---
 libcxx/include/__ranges/to.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__ranges/to.h b/libcxx/include/__ranges/to.h
index c937b0656de87..2adae5e0edbfb 100644
--- a/libcxx/include/__ranges/to.h
+++ b/libcxx/include/__ranges/to.h
@@ -28,6 +28,8 @@
 #include <__type_traits/add_pointer.h>
 #include <__type_traits/is_const.h>
 #include <__type_traits/is_volatile.h>
+#include <__type_traits/is_class.h>
+#include <__type_traits/is_union.h>
 #include <__type_traits/type_identity.h>
 #include <__utility/declval.h>
 #include <__utility/forward.h>
@@ -81,7 +83,7 @@ template <class _Container, input_range _Range, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-
+  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
   // First see if the non-recursive case applies -- the conversion target is either:
   // - a range with a convertible value type;
   // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
@@ -208,7 +210,7 @@ template <class _Container, class... _Args>
   static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
   static_assert(
       !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
-
+  static_assert(is_class_v<_Container>||is_union_v<_Container>, "The target must be a class type");
   auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail) static
     requires requires { //
       /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);

>From 44d6a6533fbc22e9dcbc2899ef631c0700564440 Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Tue, 15 Apr 2025 23:47:10 +0800
Subject: [PATCH 2/3] [libc++][ranges]test: add static_assert verification for
 ranges::to

---
 .../range.utility.conv/to.verfiy.cpp            | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
new file mode 100644
index 0000000000000..9ff01b10a759d
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
@@ -0,0 +1,17 @@
+#include <ranges>
+
+
+
+void test(){
+    struct R {
+        int* begin() const{reurn nullptr;};
+        int* end() const{return nullptr;};
+    
+        operator int() const { return 0; }
+      };
+      (void)std::ranges::to<int>(R{});
+        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+      (void)(R{} | std::ranges::to<int>());
+        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+    
+}
\ No newline at end of file

>From 7fe72642b7e7f1860673ec5364366ab91dde75ab Mon Sep 17 00:00:00 2001
From: ImoutoCon1999 <ImoutoCon1999 at outlook.com>
Date: Wed, 16 Apr 2025 18:50:11 +0800
Subject: [PATCH 3/3] format added file and correct name

---
 .../range.utility.conv/to.verfiy.cpp            | 17 -----------------
 .../range.utility.conv/to.verify.cpp            | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 17 deletions(-)
 delete mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
 create mode 100644 libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp

diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
deleted file mode 100644
index 9ff01b10a759d..0000000000000
--- a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verfiy.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#include <ranges>
-
-
-
-void test(){
-    struct R {
-        int* begin() const{reurn nullptr;};
-        int* end() const{return nullptr;};
-    
-        operator int() const { return 0; }
-      };
-      (void)std::ranges::to<int>(R{});
-        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-      (void)(R{} | std::ranges::to<int>());
-        //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
-    
-}
\ No newline at end of file
diff --git a/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
new file mode 100644
index 0000000000000..01d211df340c4
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp
@@ -0,0 +1,15 @@
+#include <ranges>
+
+void test() {
+  struct R {
+    int* begin() const { reurn nullptr; };
+    int* end() const { return nullptr; };
+
+    operator int() const { return 0; }
+  };
+  (void)std::ranges::to<int>(R{});
+  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<int>());
+  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  
+}
\ No newline at end of file



More information about the libcxx-commits mailing list