Clang Project

clang_source_code/test/CXX/dcl.decl/dcl.init/p5.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
2// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3// RUN: %clang_cc1 -fsyntax-only -verify %s
4
5//   A program that calls for default-initialization or value-initialization of
6//   an entity of reference type is illformed. If T is a cv-qualified type, the
7//   cv-unqualified version of T is used for these definitions of
8//   zero-initialization, default-initialization, and value-initialization.
9
10typedef int &IR;
11IR r; // expected-error {{declaration of reference variable 'r' requires an initializer}}
12int n = IR(); // expected-error {{reference to type 'int' requires an initializer}}
13
14#if __cplusplus < 201103L
15struct S { // expected-error {{implicit default constructor for 'S' must explicitly initialize the reference member}}
16  int &x; // expected-note {{declared here}} expected-error 3{{reference to type 'int' requires an initializer}}
17};
18S s; // expected-note {{implicit default constructor for 'S' first required here}}
19S f() {
20  return S(); // expected-note {{in value-initialization of type 'S' here}}
21}
22
23struct T
24  : S { // expected-note 2{{in value-initialization of type 'S' here}}
25};
26T t = T(); // expected-note {{in value-initialization of type 'T' here}}
27
28struct U {
29  T t[3]; // expected-note {{in value-initialization of type 'T' here}}
30};
31U u = U(); // expected-note {{in value-initialization of type 'U' here}}
32#else
33struct S {
34  int &x; // expected-note 4{{because field 'x' of reference type 'int &' would not be initialized}}
35};
36S s; // expected-error {{deleted default constructor}}
37S f() {
38  return S(); // expected-error {{deleted default constructor}}
39}
40
41struct T
42  : S { // expected-note 2{{because base class 'S' has a deleted default constructor}}
43};
44T t = T(); // expected-error {{deleted default constructor}}
45
46struct U {
47  T t[3]; // expected-note {{because field 't' has a deleted default constructor}}
48};
49U u = U(); // expected-error {{deleted default constructor}}
50#endif
51
52// Ensure that we handle C++11 in-class initializers properly as an extension.
53// In this case, there is no user-declared default constructor, so we
54// recursively apply the value-initialization checks, but we will emit a
55// constructor call anyway, because the default constructor is not trivial.
56struct V {
57  int n;
58  int &r = n; // expected-warning 0-1{{C++11}}
59};
60V v = V(); // ok
61struct W {
62  int n;
63  S s = { n }; // expected-warning 0-1{{C++11}}
64};
65W w = W(); // ok
66
67// Ensure we're not faking this up by making the default constructor
68// non-trivial.
69_Static_assert(__has_trivial_constructor(S), "");
70_Static_assert(__has_trivial_constructor(T), "");
71_Static_assert(__has_trivial_constructor(U), "");
72_Static_assert(!__has_trivial_constructor(V), "");
73_Static_assert(!__has_trivial_constructor(W), "");
74