[clang] [Clang] Implement P2280R4 Using unknown pointers and references in constant expressions (PR #95474)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 4 06:15:46 PDT 2024
================
@@ -0,0 +1,118 @@
+// RUN: %clang_cc1 -std=c++23 -verify %s
+
+using size_t = decltype(sizeof(0));
+
+namespace std {
+struct type_info {
+ const char* name() const noexcept(true);
+};
+}
+
+template <typename T, size_t N>
+constexpr size_t array_size(T (&)[N]) {
+ return N;
+}
+
+void use_array(int const (&gold_medal_mel)[2]) {
+ constexpr auto gold = array_size(gold_medal_mel); // ok
+}
+
+constexpr auto olympic_mile() {
+ const int ledecky = 1500;
+ return []{ return ledecky; };
+}
+static_assert(olympic_mile()() == 1500); // ok
+
+struct Swim {
+ constexpr int phelps() { return 28; }
+ virtual constexpr int lochte() { return 12; }
+ int coughlin = 12;
+};
+
+constexpr int how_many(Swim& swam) {
+ Swim* p = &swam;
+ return (p + 1 - 1)->phelps();
+}
+
+void splash(Swim& swam) {
+ static_assert(swam.phelps() == 28); // ok
+ static_assert((&swam)->phelps() == 28); // ok
+ Swim* pswam = &swam; // expected-note {{declared here}}
+ static_assert(pswam->phelps() == 28); // expected-error {{static assertion expression is not an integral constant expression}}
+ // expected-note at -1 {{read of non-constexpr variable 'pswam' is not allowed in a constant expression}}
+ static_assert(how_many(swam) == 28); // ok
+ static_assert(Swim().lochte() == 12); // ok
+ static_assert(swam.lochte() == 12); // expected-error {{static assertion expression is not an integral constant expression}}
----------------
tbaederr wrote:
Why does this not work?
https://github.com/llvm/llvm-project/pull/95474
More information about the cfe-commits
mailing list