Debian Codegraph @

clang/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp

raw
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions %s -fexperimental-new-constant-interpreter
3
4 // An explicitly-defaulted function may be declared constexpr only if it would
5 // have been implicitly declared as constexpr.
6 struct S1 {
7 constexpr S1() = default; // expected-error {{defaulted definition of default constructor cannot be marked constexpr}}
8 constexpr S1(const S1&) = default;
9 constexpr S1(S1&&) = default;
10 constexpr S1 &operator=(const S1&) const = default; // expected-error {{explicitly-defaulted copy assignment operator may not have}}
11 constexpr S1 &operator=(S1&&) const = default; // expected-error {{explicitly-defaulted move assignment operator may not have}}
12 constexpr ~S1() = default; // expected-error {{destructor cannot be declared constexpr}}
13 int n;
14 };
15 struct NoCopyMove {
16 constexpr NoCopyMove() {}
17 NoCopyMove(const NoCopyMove&);
18 NoCopyMove(NoCopyMove&&);
19 };
20 struct S2 {
21 constexpr S2() = default;
22 constexpr S2(const S2&) = default; // expected-error {{defaulted definition of copy constructor cannot be marked constexpr}}
23 constexpr S2(S2&&) = default; // expected-error {{defaulted definition of move constructor cannot be marked}}
24 NoCopyMove ncm;
25 };
26
27 // If a function is explicitly defaulted on its first declaration
28 // -- it is implicitly considered to be constexpr if the implicit declaration
29 // would be
30 struct S3 {
31 S3() = default;
32 S3(const S3&) = default;
33 S3(S3&&) = default;
34 constexpr S3(int n) : n(n) {}
35 int n;
36 };
37 constexpr S3 s3a = S3(0);
38 constexpr S3 s3b = s3a;
39 constexpr S3 s3c = S3();
40 constexpr S3 s3d; // expected-error {{default initialization of an object of const type 'const S3' without a user-provided default constructor}}
41
42 struct S4 {
43 S4() = default;
44 S4(const S4&) = default; // expected-note {{here}}
45 S4(S4&&) = default; // expected-note {{here}}
46 NoCopyMove ncm;
47 };
48 constexpr S4 s4a{}; // ok
49 constexpr S4 s4b = S4(); // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
50 constexpr S4 s4c = s4a; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
51
52 struct S5 {
53 constexpr S5();
54 int n = 1, m = n + 3;
55 };
56 constexpr S5::S5() = default;
57 static_assert(S5().m == 4, "");
58
59
60 // An explicitly-defaulted function may have a different exception specification
61 // from the exception specification on an implicit declaration.
62 struct E1 {
63 E1() noexcept = default;
64 E1(const E1&) noexcept = default;
65 E1(E1&&) noexcept = default;
66 E1 &operator=(const E1&) noexcept = default;
67 E1 &operator=(E1&&) noexcept = default;
68 ~E1() noexcept = default;
69 };
70 struct E2 {
71 E2() noexcept(false) = default;
72 E2(const E2&) noexcept(false) = default;
73 E2(E2&&) noexcept(false) = default;
74 E2 &operator=(const E2&) noexcept(false) = default;
75 E2 &operator=(E2&&) noexcept(false) = default;
76 ~E2() noexcept(false) = default;
77 };
78 E2 e2;
79 E2 make_e2() noexcept;
80 void take_e2(E2&&) noexcept;
81 static_assert(!noexcept(E2()), "");
82 static_assert(!noexcept(E2(e2)), "");
83 static_assert(!noexcept(E2(static_cast<E2&&>(e2))), "");
84 static_assert(!noexcept(e2 = e2), "");
85 static_assert(!noexcept(e2 = static_cast<E2&&>(e2)), "");
86 // FIXME: This expression results in destruction of an E2 temporary; the
87 // noexcept expression should evaluate to false.
88 static_assert(noexcept(take_e2(make_e2())), "");
89
90 // If a function is explicitly defaulted on its first declaration
91 // -- it is implicitly considered to have the same exception-specification as
92 // if it had been implicitly declared
93 struct E3 {
94 E3() = default;
95 E3(const E3&) = default;
96 E3(E3&&) = default;
97 E3 &operator=(const E3&) = default;
98 E3 &operator=(E3&&) = default;
99 ~E3() = default;
100 };
101 E3 e3;
102 static_assert(noexcept(E3(), E3(E3()), E3(e3), e3 = E3(), e3 = e3), "");
103 struct E4 {
104 E4() noexcept(false);
105 E4(const E4&) noexcept(false);
106 E4(E4&&) noexcept(false);
107 E4 &operator=(const E4&) noexcept(false);
108 E4 &operator=(E4&&) noexcept(false);
109 ~E4() noexcept(false);
110 };
111 struct E5 {
112 E5() = default;
113 E5(const E5&) = default;
114 E5(E5&&) = default;
115 E5 &operator=(const E5&) = default;
116 E5 &operator=(E5&&) = default;
117 ~E5() = default;
118
119 E4 e4;
120 };
121 E5 e5;
122 static_assert(!noexcept(E5()), "");
123 static_assert(!noexcept(E5(static_cast<E5&&>(e5))), "");
124 static_assert(!noexcept(E5(e5)), "");
125 static_assert(!noexcept(e5 = E5()), "");
126 static_assert(!noexcept(e5 = e5), "");
127
128 namespace PR13492 {
129 struct B {
130 B() = default;
131 int field;
132 };
133
134 void f() {
135 const B b; // expected-error {{default initialization of an object of const type 'const B' without a user-provided default constructor}}
136 }
137 }