• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // XFAIL: *
3 
4 struct notlit {
notlitnotlit5   notlit() {}
6 };
7 struct notlit2 {
notlit2notlit28   notlit2() {}
9 };
10 
11 // valid declarations
12 constexpr int i1 = 0;
f1()13 constexpr int f1() { return 0; }
14 struct s1 {
15   constexpr static int mi = 0;
16 };
17 
18 // invalid declarations
19 // not a definition of an object
20 constexpr extern int i2; // x
21 // not a literal type
22 constexpr notlit nl1; // x
23 // function parameters
f2(constexpr int i)24 void f2(constexpr int i) {} // x
25 // non-static member
26 struct s2 {
27   constexpr int mi; // x
28 };
29 // redeclaration mismatch
30 constexpr int f3(); // n
31 int f3(); // x
32 int f4(); // n
33 constexpr int f4(); // x
34 
35 // template stuff
36 template <typename T>
ft(T t)37 constexpr T ft(T t) { return t; }
38 
39 // specialization can differ in constepxr
40 template <>
ft(notlit nl)41 notlit ft(notlit nl) { return nl; }
42 
43 constexpr int i3 = ft(1);
44 
test()45 void test() {
46   // ignore constexpr when instantiating with non-literal
47   notlit2 nl2;
48   (void)ft(nl2);
49 }
50 
51 // Examples from the standard:
52 constexpr int square(int x);
53 constexpr int bufsz = 1024;
54 
55 constexpr struct pixel { // x
56   int x;
57   int y;
58   constexpr pixel(int);
59 };
60 
pixel(int a)61 constexpr pixel::pixel(int a)
62   : x(square(a)), y(square(a))
63   { }
64 
65 constexpr pixel small(2); // x (no definition of square(int) yet, so can't
66                           // constexpr-eval pixel(int))
67 
square(int x)68 constexpr int square(int x) {
69   return x * x;
70 }
71 
72 constexpr pixel large(4); // now valid
73 
next(constexpr int x)74 int next(constexpr int x) { // x
75       return x + 1;
76 }
77 
78 extern constexpr int memsz; // x
79