summaryrefslogtreecommitdiffstats
path: root/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p4.cpp
blob: 9218bcf45bb4721eec495d1491c4125e87d1a480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// RUN: %clang_cc1 -verify -std=c++11 -fcxx-exceptions %s

namespace N {
  typedef char C;
}

namespace M {
  typedef double D;
}

struct NonLiteral { // expected-note 2{{no constexpr constructors}}
  NonLiteral() {}
  NonLiteral(int) {}
};
struct Literal {
  constexpr Literal() {}
  operator int() const { return 0; }
};

// Note, the wording applies constraints to the definition of constexpr
// constructors, but we intentionally apply all that we can to the declaration
// instead. See DR1360.

// In the definition of a constexpr constructor, each of the parameter types
// shall be a literal type.
struct S {
  constexpr S(int, N::C);
  constexpr S(int, NonLiteral, N::C); // expected-error {{constexpr constructor's 2nd parameter type 'NonLiteral' is not a literal type}}
  constexpr S(int, NonLiteral = 42); // expected-error {{constexpr constructor's 2nd parameter type 'NonLiteral' is not a literal type}}

  // In addition, either its function-body shall be = delete or = default
  constexpr S() = default;
  constexpr S(Literal) = delete;
};

// or it shall satisfy the following constraints:

// - the class shall not have any virtual base classes;
struct T : virtual S { // expected-note {{here}}
  constexpr T(); // expected-error {{constexpr constructor not allowed in struct with virtual base classes}}
};
namespace IndirectVBase {
  struct A {};
  struct B : virtual A {}; // expected-note {{here}}
  class C : public B {
  public:
    constexpr C(); // expected-error {{constexpr constructor not allowed in class with virtual base classes}}
  };
}

// - its function-body shall not be a function-try-block;
struct U {
  constexpr U()
    try // expected-error {{function try block not allowed in constexpr constructor}}
    : u() {
  } catch (...) {
    throw;
  }
  int u;
};

// - the compound-statememt of its function-body shall contain only
struct V {
  constexpr V() {
    //  - null statements,
    ;

    //  - static_assert-declarations,
    static_assert(true, "the impossible happened!");

    //  - typedef declarations and alias-declarations that do not define classes
    //    or enumerations,
    typedef int I;
    typedef struct S T;
    using J = int;
    using K = int[sizeof(I) + sizeof(J)];
    // Note, the standard requires we reject this.
    struct U;

    //  - using-declarations,
    using N::C;

    //  - and using-directives;
    using namespace N;
  }

  constexpr V(int(&)[1]) {
    for (int n = 0; n < 10; ++n) // expected-error {{statement not allowed in constexpr constructor}}
      /**/;
  }
  constexpr V(int(&)[2]) {
    constexpr int a = 0; // expected-error {{variables cannot be declared in a constexpr constructor}}
  }
  constexpr V(int(&)[3]) {
    constexpr int ForwardDecl(int); // expected-error {{statement not allowed in constexpr constructor}}
  }
  constexpr V(int(&)[4]) {
    typedef struct { } S1; // expected-error {{types cannot be defined in a constexpr constructor}}
  }
  constexpr V(int(&)[5]) {
    using S2 = struct { }; // expected-error {{types cannot be defined in a constexpr constructor}}
  }
  constexpr V(int(&)[6]) {
    struct S3 { }; // expected-error {{types cannot be defined in a constexpr constructor}}
  }
  constexpr V(int(&)[7]) {
    return; // expected-error {{statement not allowed in constexpr constructor}}
  }
};

// - every non-static data member and base class sub-object shall be initialized
struct W {
  int n; // expected-note {{member not initialized by constructor}}
  constexpr W() {} // expected-error {{constexpr constructor must initialize all members}}
};
struct AnonMembers {
  int a; // expected-note {{member not initialized by constructor}}
  union { // expected-note 2{{member not initialized by constructor}}
    char b;
    struct {
      double c;
      long d; // expected-note {{member not initialized by constructor}}
    };
    union {
      char e;
      void *f;
    };
  };
  struct { // expected-note {{member not initialized by constructor}}
    long long g;
    struct {
      int h; // expected-note {{member not initialized by constructor}}
      double i; // expected-note {{member not initialized by constructor}}
    };
    union { // expected-note 2{{member not initialized by constructor}}
      char *j;
      AnonMembers *k;
    };
  };

  constexpr AnonMembers(int(&)[1]) : a(), b(), g(), h(), i(), j() {} // ok
  // missing d, i, j/k union
  constexpr AnonMembers(int(&)[2]) : a(), c(), g(), h() {} // expected-error {{constexpr constructor must initialize all members}}
  constexpr AnonMembers(int(&)[3]) : a(), e(), g(), h(), i(), k() {} // ok
  // missing h, j/k union
  constexpr AnonMembers(int(&)[4]) : a(), c(), d(), g(), i() {} // expected-error {{constexpr constructor must initialize all members}}
  // missing b/c/d/e/f union
  constexpr AnonMembers(int(&)[5]) : a(), g(), h(), i(), k() {} // expected-error {{constexpr constructor must initialize all members}}
  // missing a, b/c/d/e/f union, g/h/i/j/k struct
  constexpr AnonMembers(int(&)[6]) {} // expected-error {{constexpr constructor must initialize all members}}
};

template<typename T> using Int = int;
template<typename T>
struct TemplateInit {
  T a;
  int b; // desired-note {{not initialized}}
  Int<T> c; // desired-note {{not initialized}}
  struct {
    T d;
    int e; // desired-note {{not initialized}}
    Int<T> f; // desired-note {{not initialized}}
  };
  struct {
    Literal l;
    Literal m;
    Literal n[3];
  };
  union { // desired-note {{not initialized}}
    T g;
    T h;
  };
  // FIXME: This is ill-formed (no diagnostic required). We should diagnose it.
  constexpr TemplateInit() {} // desired-error {{must initialize all members}}
};
template<typename T> struct TemplateInit2 {
  Literal l;
  constexpr TemplateInit2() {} // ok
};

template<typename T> struct weak_ptr {
  constexpr weak_ptr() : p(0) {}
  T *p;
};
template<typename T> struct enable_shared_from_this {
  weak_ptr<T> weak_this;
  constexpr enable_shared_from_this() {} // ok
};
constexpr int f(enable_shared_from_this<int>);

// - every constructor involved in initializing non-static data members and base
//   class sub-objects shall be a constexpr constructor.
//
// FIXME: Implement this as part of the 'must be able to produce a constant
// expression' rules.

// - every assignment-expression that is an initializer-caluse appearing
//   directly or indirectly within a brace-or-equal-initializer for a non-static
//   data member that is not named by a mem-initializer-id shall be a constant
//   expression; and
//
// Note, we deliberately do not implement this bullet, so that we can allow the
// following example. (See N3308).
struct X {
  int a = 0;
  int b = 2 * a + 1; // ok, not a constant expression.

  constexpr X() {}
  constexpr X(int c) : a(c) {} // ok, b initialized by 2 * c + 1
};

//  - every implicit conversion used in converting a constructor argument to the
//    corresponding parameter type and converting a full-expression to the
//    corresponding member type shall be one of those allowed in a constant
//    expression.
//
// We implement the proposed resolution of DR1364 and ignore this bullet.


namespace StdExample {
  struct Length {
    explicit constexpr Length(int i = 0) : val(i) { }
  private:
      int val;
  };
}
OpenPOWER on IntegriCloud