Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
      2 
      3 struct Bitfield {
      4   int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}}
      5 };
      6 
      7 int a;
      8 class NoWarning {
      9   int &n = a;
     10 public:
     11   int &GetN() { return n; }
     12 };
     13 
     14 bool b();
     15 int k;
     16 struct Recurse {
     17   int &n = // expected-error {{cannot use defaulted default constructor of 'Recurse' within the class outside of member functions because 'n' has an initializer}}
     18       b() ?
     19       Recurse().n : // expected-note {{implicit default constructor for 'Recurse' first required here}}
     20       k;
     21 };
     22 
     23 struct UnknownBound {
     24   int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
     25   int bs[4] = { 4, 5, 6, 7 };
     26   int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
     27 };
     28 
     29 template<int n> struct T { static const int B; };
     30 template<> struct T<2> { template<int C, int D> using B = int; };
     31 const int C = 0, D = 0;
     32 struct S {
     33   int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
     34   T<sizeof(as) / sizeof(int)> x;
     35   // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
     36   operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
     37                       // expected-error {{array bound cannot be deduced from an in-class initializer}}
     38 };
     39 
     40 struct ThrowCtor { ThrowCtor(int) noexcept(false); };
     41 struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
     42 
     43 struct Throw { ThrowCtor tc = 42; };
     44 struct NoThrow { NoThrowCtor tc = 42; };
     45 
     46 static_assert(!noexcept(Throw()), "incorrect exception specification");
     47 static_assert(noexcept(NoThrow()), "incorrect exception specification");
     48 
     49 struct CheckExcSpec {
     50   CheckExcSpec() noexcept(true) = default;
     51   int n = 0;
     52 };
     53 struct CheckExcSpecFail {
     54   CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
     55   ThrowCtor tc = 123;
     56 };
     57 
     58 struct TypedefInit {
     59   typedef int A = 0; // expected-error {{illegal initializer}}
     60 };
     61 
     62 // PR10578 / <rdar://problem/9877267>
     63 namespace PR10578 {
     64   template<typename T>
     65   struct X {
     66     X() {
     67       T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
     68     }
     69   };
     70 
     71   struct Y : X<int> {
     72     Y();
     73   };
     74 
     75   Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
     76   } catch(...) {
     77   }
     78 }
     79 
     80 namespace PR14838 {
     81   struct base { ~base() {} };
     82   class function : base {
     83     ~function() {} // expected-note {{implicitly declared private here}}
     84   public:
     85     function(...) {}
     86   };
     87   struct thing {};
     88   struct another {
     89     another() : r(thing()) {}
     90     // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}}
     91     // expected-warning@-2 {{binding reference member 'r' to a temporary value}}
     92     const function &r; // expected-note {{reference member declared here}}
     93   } af;
     94 }
     95 
     96 namespace rdar14084171 {
     97   struct Point { // expected-note 3 {{candidate constructor}}
     98     double x;
     99     double y;
    100   };
    101   struct Sprite {
    102     Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
    103   };
    104   void f(Sprite& x) { x = x; }
    105 }
    106 
    107 namespace PR18560 {
    108   struct X { int m; };
    109 
    110   template<typename T = X,
    111            typename U = decltype(T::m)>
    112   int f();
    113 
    114   struct Y { int b = f(); };
    115 }
    116 
    117 namespace template_valid {
    118 // Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
    119 struct A {
    120   A();
    121   template <typename T>
    122   struct B { int m1 = sizeof(A) + sizeof(T); };
    123   B<int> m2;
    124 };
    125 A::A() {}
    126 }
    127 
    128 namespace template_default_ctor {
    129 struct A {
    130   template <typename T>
    131   struct B {
    132     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
    133   };
    134   // expected-note@+1 {{implicit default constructor for 'template_default_ctor::A::B<int>' first required here}}
    135   enum { NOE = noexcept(B<int>()) };
    136 };
    137 }
    138 
    139 namespace default_ctor {
    140 struct A {
    141   struct B {
    142     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
    143   };
    144   // expected-note@+1 {{implicit default constructor for 'default_ctor::A::B' first required here}}
    145   enum { NOE = noexcept(B()) };
    146 };
    147 }
    148 
    149 namespace member_template {
    150 struct A {
    151   template <typename T>
    152   struct B {
    153     struct C {
    154       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'C' within 'A' outside of member functions because 'm1' has an initializer}}
    155     };
    156     template <typename U>
    157     struct D {
    158       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'D' within 'A' outside of member functions because 'm1' has an initializer}}
    159     };
    160   };
    161   enum {
    162     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::C' first required here}}
    163     NOE1 = noexcept(B<int>::C()),
    164     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::D<int>' first required here}}
    165     NOE2 = noexcept(B<int>::D<int>())
    166   };
    167 };
    168 }
    169 
    170 namespace explicit_instantiation {
    171 template<typename T> struct X {
    172   X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
    173   int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
    174 };
    175 template struct X<int>; // ok
    176 template<typename T> X<T>::X() {}
    177 template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
    178 }
    179 
    180 namespace local_class {
    181 template<typename T> void f() {
    182   struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
    183     int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
    184   };
    185 }
    186 void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
    187 }
    188 
    189 namespace PR22056 {
    190 template <int N>
    191 struct S {
    192   int x[3] = {[N] = 3};
    193 };
    194 }
    195 
    196 namespace PR28060 {
    197 template <class T>
    198 void foo(T v) {
    199   struct s {
    200     T *s = 0;
    201   };
    202 }
    203 template void foo(int);
    204 }
    205