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

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Thu Apr 17 06:29:09 PDT 2025


================
@@ -0,0 +1,79 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Test that the "mandates" requirements on the given class are checked using `static_assert`.
+#include <ranges>
+void ff() {}
+void test() {
+  struct C {
+    int member;
+    int f() { return 0; }
+  };
+
+  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 { return 0; }
+    operator int*() const { return nullptr; }
+    operator func_ptr() const { return nullptr; }
+    operator void() const {}
+    operator member_func_ptr() const { return nullptr; }
+    operator member_ptr() const { return nullptr; }
+    operator color() const { return color::red; }
+  };
+  (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}}
+  (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}}
+  (void)std::ranges::to<func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+
+  (void)std::ranges::to<void>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   void>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<member_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             member_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<member_func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             member_func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_ptr>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} |
+         std::ranges::to<
+             func_ptr>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<color>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   color>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)std::ranges::to<func_t>(
+      R{}); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
+  (void)(R{} | std::ranges::to<
+                   func_t>()); //expected-error-re@*:* {{static assertion failed{{.*}}The target must be a class type}}
----------------
frederick-vs-ja wrote:

Looks like that expected error messages should be different for these lines.
```
# | error: 'expected-error' diagnostics seen but not expected:
# |   File /home/buildkite/.buildkite-agent/builds/freebsd-test-1/llvm-project/libcxx-ci/build/generic-cxx26/libcxx/test-suite-install/include/c++/v1/__ranges/to.h Line 130: static assertion failed: ranges::to: unable to convert to the given container type.
# | error: 'expected-warning' diagnostics seen but not expected:
# |   File /usr/home/buildkite/.buildkite-agent/builds/freebsd-test-1/llvm-project/libcxx-ci/libcxx/test/libcxx/ranges/range.utility/range.utility.conv/to.verify.cpp Line 33: conversion function converting 'R' to 'void' will never be used
```

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


More information about the libcxx-commits mailing list