OSDN Git Service

* c-decl.c (shadow_tag_warned, grokdeclarator): Handle _Alignas
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.dg / cpp0x / constexpr-pos1.C
1 // Positive examples from N3092 (FCD)
2 // { dg-options -std=c++0x }
3
4 #define SA(X) static_assert(X, #X)
5
6 constexpr int bufsz = 1024; // OK: definition
7 SA (bufsz == 1024);
8
9 constexpr int square(int x); // OK: declaration
10
11 struct pixel {
12   int x;
13   int y;
14   // OK: declaration
15   constexpr pixel(int);
16 };
17 constexpr pixel::pixel(int a) // OK: definition
18   : x(square(a)), y(square(a))
19 { }
20
21 constexpr int square(int x) // OK: definition
22 { return x * x; }
23
24 constexpr pixel large(4); // OK: square defined
25 SA(large.x == 16 && large.y==16);
26
27 constexpr long long_max() // OK
28 { return 2147483647; }
29
30 SA(long_max() == 2147483647);
31
32 constexpr int abs(int x) // OK
33 { return x < 0 ? -x : x; }
34
35 SA(abs(-1) == 1);
36 SA(abs(24) == 24);
37
38 struct Length {
39   explicit constexpr Length(int i = 0) : val(i) { }
40 private:
41   int val;
42 };
43
44 constexpr Length l1;
45 constexpr Length l2(12);
46
47 struct pixel2 {
48   int x, y;
49 };
50 constexpr pixel2 ur = { 1294, 1024 };// OK
51
52 SA(ur.x == 1294 && ur.y == 1024);
53
54 constexpr const int* addr(const int& ir) { return &ir; } // OK
55 static const int x = 5;
56 extern constexpr const int* xp = addr(x); // OK: (const int*)&(const int&)x
57                                           // is an address contant expression
58 SA(xp == &x);
59 extern constexpr int x2 = *addr(5);
60 SA(x2 == 5);