[libcxx-commits] [libcxx] [libc++][ranges] Reject non-class types in ranges::to (PR #135802)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 23 01:28:05 PDT 2025


================
@@ -14,12 +14,79 @@
 #include <ranges>
 #include <vector>
 
-void test() {
+void test_cv_qualifications() {
   using R = std::vector<int>;
-  R in = {1, 2, 3};
+  R in    = {1, 2, 3};
 
-  (void)std::ranges::to<const R>(in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
-  (void)(in | std::ranges::to<const R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
-  (void)std::ranges::to<volatile R>(in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
-  (void)(in | std::ranges::to<volatile R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+  (void)std::ranges::to<const R>(
+      in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
+  (void)(in |
+         std::ranges::to<
+             const R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be const-qualified, please remove the const}}
+  (void)std::ranges::to<volatile R>(
+      in); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+  (void)(in |
+         std::ranges::to<
+             volatile R>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target container cannot be volatile-qualified, please remove the volatile}}
+}
+//unexpected_types
+void ff();
+void test_unexpected_types() {
+  struct C {
+    int member;
+    int f();
+  };
+
+  enum color { red, green, blue };
+  using member_func_ptr = decltype(&C::f);
+  using member_ptr      = decltype(&C::member);
+  using func_ptr        = decltype(&ff);
+  using func_t          = decltype(ff);
+
+  struct R {
+    int* begin() const { return nullptr; };
+    int* end() const { return nullptr; };
+
+    operator int() const;
+    operator int*() const;
+    operator func_ptr() const;
+    operator member_func_ptr() const;
+    operator member_ptr() const;
+    operator color() const;
+  };
+  (void)std::ranges::to<int>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
----------------
philnik777 wrote:

```suggestion
  //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
  (void)std::ranges::to<int>(R{});
```
Throughout, I think that makes this a lot nicer to read.

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


More information about the libcxx-commits mailing list