p2.cpp revision 6180245e9f63d2927b185ec251fb75aba30f1cac
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
3// An explicitly-defaulted function may be declared constexpr only if it would
4// have been implicitly declared as constexpr.
5struct S1 {
6  constexpr S1() = default; // expected-error {{defaulted definition of default constructor is not constexpr}}
7  constexpr S1(const S1&) = default;
8  constexpr S1(S1&&) = default;
9  constexpr S1 &operator=(const S1&) = default; // expected-error {{explicitly-defaulted copy assignment operator may not have}}
10  constexpr S1 &operator=(S1&&) = default; // expected-error {{explicitly-defaulted move assignment operator may not have}}
11  constexpr ~S1() = default; // expected-error {{destructor cannot be marked constexpr}}
12  int n;
13};
14struct NoCopyMove {
15  constexpr NoCopyMove() {}
16  NoCopyMove(const NoCopyMove&);
17  NoCopyMove(NoCopyMove&&);
18};
19struct S2 {
20  constexpr S2() = default;
21  constexpr S2(const S2&) = default; // expected-error {{defaulted definition of copy constructor is not constexpr}}
22  constexpr S2(S2&&) = default; // expected-error {{defaulted definition of move constructor is not constexpr}}
23  NoCopyMove ncm;
24};
25
26// If a function is explicitly defaulted on its first declaration
27//   -- it is implicitly considered to be constexpr if the implicit declaration
28//      would be
29struct S3 {
30  S3() = default; // expected-note {{here}}
31  S3(const S3&) = default;
32  S3(S3&&) = default;
33  constexpr S3(int n) : n(n) {}
34  int n;
35};
36constexpr S3 s3a = S3(0);
37constexpr S3 s3b = s3a;
38constexpr S3 s3c = S3(); // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
39
40struct S4 {
41  S4() = default;
42  S4(const S4&) = default; // expected-note {{here}}
43  S4(S4&&) = default; // expected-note {{here}}
44  NoCopyMove ncm;
45};
46constexpr S4 s4a; // ok
47constexpr S4 s4b = S4(); // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
48constexpr S4 s4c = s4a; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
49
50struct S5 {
51  constexpr S5();
52  int n = 1, m = n + 3;
53};
54constexpr S5::S5() = default;
55static_assert(S5().m == 4, "");
56