Home | History | Annotate | Download | only in dcl.constexpr
      1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
      2 
      3 // A constexpr specifier used in an object declaration declares the object as
      4 // const.
      5 constexpr int a = 0;
      6 extern const int a;
      7 
      8 int i; // expected-note 2{{here}}
      9 constexpr int *b = &i;
     10 extern int *const b;
     11 
     12 constexpr int &c = i;
     13 extern int &c;
     14 
     15 constexpr int (*d)(int) = 0;
     16 extern int (*const d)(int);
     17 
     18 // A variable declaration which uses the constexpr specifier shall have an
     19 // initializer and shall be initialized by a constant expression.
     20 constexpr int ni1; // expected-error {{default initialization of an object of const type 'const int'}}
     21 constexpr struct C { C(); } ni2; // expected-error {{cannot have non-literal type 'const struct C'}} expected-note 3{{has no constexpr constructors}}
     22 constexpr double &ni3; // expected-error {{declaration of reference variable 'ni3' requires an initializer}}
     23 
     24 constexpr int nc1 = i; // expected-error {{constexpr variable 'nc1' must be initialized by a constant expression}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
     25 constexpr C nc2 = C(); // expected-error {{cannot have non-literal type 'const C'}}
     26 int &f(); // expected-note {{declared here}}
     27 constexpr int &nc3 = f(); // expected-error {{constexpr variable 'nc3' must be initialized by a constant expression}} expected-note {{non-constexpr function 'f' cannot be used in a constant expression}}
     28 constexpr int nc4(i); // expected-error {{constexpr variable 'nc4' must be initialized by a constant expression}} expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
     29 constexpr C nc5((C())); // expected-error {{cannot have non-literal type 'const C'}}
     30 int &f(); // expected-note {{here}}
     31 constexpr int &nc6(f()); // expected-error {{constexpr variable 'nc6' must be initialized by a constant expression}} expected-note {{non-constexpr function 'f'}}
     32 
     33 struct pixel {
     34   int x, y;
     35 };
     36 constexpr pixel ur = { 1294, 1024 }; // ok
     37 constexpr pixel origin;              // expected-error {{default initialization of an object of const type 'const pixel' without a user-provided default constructor}}
     38