Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / dcl.decl / dcl.init / p5.cpp
blobe24b7f71ff6f52b62210003c9a882e7c484f8794
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-deprecated-builtins -std=c++98 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-deprecated-builtins -std=c++11 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-deprecated-builtins %s
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.
10 typedef int &IR;
11 IR r; // expected-error {{declaration of reference variable 'r' requires an initializer}}
12 int n = IR(); // expected-error {{reference to type 'int' requires an initializer}}
14 #if __cplusplus < 201103L
15 struct 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}}
18 S s; // expected-note {{implicit default constructor for 'S' first required here}}
19 S f() {
20 return S(); // expected-note {{in value-initialization of type 'S' here}}
23 struct T
24 : S { // expected-note 2{{in value-initialization of type 'S' here}}
26 T t = T(); // expected-note {{in value-initialization of type 'T' here}}
28 struct U {
29 T t[3]; // expected-note {{in value-initialization of type 'T' here}}
31 U u = U(); // expected-note {{in value-initialization of type 'U' here}}
32 #else
33 struct S {
34 int &x; // expected-note 4{{because field 'x' of reference type 'int &' would not be initialized}}
36 S s; // expected-error {{deleted default constructor}}
37 S f() {
38 return S(); // expected-error {{deleted default constructor}}
41 struct T
42 : S { // expected-note 2{{because base class 'S' has a deleted default constructor}}
44 T t = T(); // expected-error {{deleted default constructor}}
46 struct U {
47 T t[3]; // expected-note {{because field 't' has a deleted default constructor}}
49 U u = U(); // expected-error {{deleted default constructor}}
50 #endif
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.
56 struct V {
57 int n;
58 int &r = n; // expected-warning 0-1{{C++11}}
60 V v = V(); // ok
61 struct W {
62 int n;
63 S s = { n }; // expected-warning 0-1{{C++11}}
65 W w = W(); // ok
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), "");