Debian Codegraph @

clang/test/SemaCXX/constexpr-value-init.cpp

raw
1 // RUN: %clang_cc1 %s -Wno-uninitialized -std=c++17 -fsyntax-only -verify
2 // RUN: %clang_cc1 %s -Wno-uninitialized -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter
3
4 struct A {
5 constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}
6 int a;
7 int b;
8 };
9 struct B { // expected-note {{in call to 'A()'}}
10 A a;
11 };
12
13 constexpr A a1; // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
14 constexpr A a2 = A(); // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
15 void f() {
16 constexpr A a; // expected-error {{constant expression}} expected-note {{in call to 'A()'}}
17 }
18
19 constexpr B b1; // expected-error {{constant expression}} expected-note {{in call to 'B()'}}
20 constexpr B b2 = B(); // ok
21 static_assert(b2.a.a == 1, "");
22 static_assert(b2.a.b == 2, "");
23
24 struct C {
25 int c;
26 };
27 struct D : C { int d; };
28 constexpr C c1; // expected-error {{without a user-provided default constructor}}
29 constexpr C c2 = C(); // ok
30 constexpr D d1; // expected-error {{without a user-provided default constructor}}
31 constexpr D d2 = D(); // ok with DR1452
32 static_assert(D().c == 0, "");
33 static_assert(D().d == 0, "");
34
35 struct V : virtual C {};
36 template<typename T> struct Z : T {
37 constexpr Z() : V() {}
38 };
39 constexpr int n = Z<V>().c; // expected-error {{constant expression}} expected-note {{non-literal type 'Z<V>'}}
40
41 struct E { // expected-note {{in call to 'A()'}}
42 A a[2];
43 };
44 constexpr E e1; // expected-error {{constant expression}} expected-note {{in call to 'E()'}}
45 constexpr E e2 = E();
46 static_assert(e2.a[0].a == 1, "");
47 static_assert(e2.a[0].b == 2, "");
48 static_assert(e2.a[1].a == 1, "");
49 static_assert(e2.a[1].b == 2, "");
50
51 namespace InvalidDeclInsideConstExpr {
52 template <int a> struct i; // expected-note {{template is declared here}}
53 template <> struct i<0> {};
54
55 template <int x> constexpr auto c() {
56 // i<x> is valid, but it might be incomplete. g would be invalid in that case.
57 i<x> g; // expected-error {{implicit instantiation of undefined template 'InvalidDeclInsideConstExpr::i<1>'}}
58 return 0;
59 }
60
61 auto y = c<1>(); // expected-note {{in instantiation of function template specialization 'InvalidDeclInsideConstExpr::c<1>' requested here}}
62 auto x = c<0>(); // this is valid.
63 }