Home | History | Annotate | Download | only in dcl.init
      1 // RUN: %clang_cc1 -fsyntax-only -verify %s
      2 
      3 //   A program that calls for default-initialization or value-initialization of
      4 //   an entity of reference type is illformed. If T is a cv-qualified type, the
      5 //   cv-unqualified version of T is used for these definitions of
      6 //   zero-initialization, default-initialization, and value-initialization.
      7 
      8 struct S { // expected-error {{implicit default constructor for 'S' must explicitly initialize the reference member}}
      9   int &x; // expected-note {{declared here}} expected-error 3{{reference to type 'int' requires an initializer}}
     10 };
     11 S s; // expected-note {{implicit default constructor for 'S' first required here}}
     12 S f() {
     13   return S(); // expected-note {{in value-initialization of type 'S' here}}
     14 }
     15 
     16 struct T
     17   : S { // expected-note 2{{in value-initialization of type 'S' here}}
     18 };
     19 T t = T(); // expected-note {{in value-initialization of type 'T' here}}
     20 
     21 struct U {
     22   T t[3]; // expected-note {{in value-initialization of type 'T' here}}
     23 };
     24 U u = U(); // expected-note {{in value-initialization of type 'U' here}}
     25 
     26 // Ensure that we handle C++11 in-class initializers properly as an extension.
     27 // In this case, there is no user-declared default constructor, so we
     28 // recursively apply the value-initialization checks, but we will emit a
     29 // constructor call anyway, because the default constructor is not trivial.
     30 struct V {
     31   int n;
     32   int &r = n; // expected-warning {{C++11}}
     33 };
     34 V v = V(); // ok
     35 struct W {
     36   int n;
     37   S s = { n }; // expected-warning {{C++11}}
     38 };
     39 W w = W(); // ok
     40 
     41 // Ensure we're not faking this up by making the default constructor
     42 // non-trivial.
     43 #define static_assert(B, S) typedef int assert_failed[(B) ? 1 : -1];
     44 static_assert(__has_trivial_constructor(S), "");
     45 static_assert(__has_trivial_constructor(T), "");
     46 static_assert(__has_trivial_constructor(U), "");
     47 static_assert(!__has_trivial_constructor(V), "");
     48 static_assert(!__has_trivial_constructor(W), "");
     49