// RUN: %clang_cc1 -std=c++20 -fcxx-exceptions -verify %s
struct A { int n; };
template struct B {
static constexpr A &v = a; // expected-error {{binding reference of type 'A' to value of type 'const A' drops 'const' qualifier}}
};
template struct C {
static constexpr const A &v = a;
};
// All such template parameters in the program of the same type with the same
// value denote the same template parameter object.
template void check() {
static_assert(&a == &T::v); // expected-error {{failed}}
}
using T = C;
template void check();
template void check(); // expected-note {{instantiation of}}
// Different types with the same value are unequal.
struct A2 { int n; };
template struct C2 {
static constexpr const A2 &v = a2;
};
static_assert((void*)&C::v != (void*)&C2::v);
// A template parameter object shall have constant destruction.
namespace ConstDestruction {
struct D {
int n;
bool can_destroy;
constexpr ~D() {
if (!can_destroy)
throw "oh no"; // expected-note {{subexpression not valid}}
}
};
template
void f() {} // expected-note 2{{invalid explicitly-specified argument}}
void g() {
f();
f(); // expected-error {{no matching function}}
}
// We can SFINAE on constant destruction.
template auto h(T t) -> decltype(f());
template auto h(T t) -> decltype(f());
void i() {
h(D());
// Ensure we don't cache an invalid template argument after we've already
// seen it in a SFINAE context.
f(); // expected-error {{no matching function}}
f();
}
template struct Z {};
Z z1;
Z z2; // expected-error {{non-type template argument is not a constant expression}} expected-note-re {{in call to '{{.*}}->~D()'}}
}