* decl.c (make_typename_type): s/parameters/arguments/.
[official-gcc.git] / gcc / testsuite / g++.dg / cpp0x / addressof1.C
blobbe4b5e81d8971dbfda2f4894fb81243ffeaa35aa
1 // LWG2296 - addressof should be constexpr
2 // { dg-do run { target c++11 } }
4 template <typename T>
5 constexpr inline T *
6 addressof (T &x) noexcept
8   return __builtin_addressof (x);
11 int i;
12 static_assert (__builtin_addressof (i) == &i, "");
13 static_assert (addressof (i) == &i, "");
15 constexpr int &j = i;
16 static_assert (__builtin_addressof (j) == &i, "");
17 static_assert (addressof (j) == &i, "");
19 struct S { int s; } s;
20 static_assert (__builtin_addressof (s) == &s, "");
21 static_assert (addressof (s) == &s, "");
23 struct T
25   static T tt;
26   constexpr T () : p (addressof (tt)) {}
27   constexpr T *operator & () const { return p; }
28   T *p;
30 constexpr T t;
31 T T::tt;
32 static_assert (&t == __builtin_addressof (T::tt), "");
33 static_assert (&t == addressof (T::tt), "");
35 struct S x, y;
37 constexpr S *
38 foo (bool b)
40   return __builtin_addressof (b ? x : y);
43 constexpr S *
44 bar (bool b, S &c, S &d)
46   return __builtin_addressof (b ? c : d);
49 static_assert (foo (false) == &y, "");
50 static_assert (foo (true) == &x, "");
51 static_assert (bar (false, y, x) == &x, "");
52 static_assert (bar (true, y, x) == &y, "");
54 constexpr S *
55 foo2 (bool b)
57   return addressof (b ? x : y);
60 constexpr S *
61 bar2 (bool b, S &c, S &d)
63   return addressof (b ? c : d);
66 static_assert (foo2 (false) == &y, "");
67 static_assert (foo2 (true) == &x, "");
68 static_assert (bar2 (false, y, x) == &x, "");
69 static_assert (bar2 (true, y, x) == &y, "");
71 constexpr int a = 1;
72 static_assert (__builtin_addressof (a) == &a, "");
73 static_assert (addressof (a) == &a, "");
74 constexpr int c[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
76 void
77 baz ()
81 int
82 main ()
84   if (__builtin_addressof (T::tt) == __builtin_addressof (t)
85       || addressof (T::tt) == addressof (t)
86       || &T::tt != &t
87       || __builtin_addressof (baz) != baz
88       || addressof (baz) != baz)
89     __builtin_abort ();
91   // reinterpret casts are not constexprs
92   if (! (((int *) __builtin_addressof (s) == &s.s)
93          && ((int *) addressof (s) == &s.s)
94          && (__builtin_addressof (t) == (const T *) &t.p)
95          && (addressof (t) == (const T *) &t.p)
96          && ((const int *) __builtin_addressof (c) == &c[0])
97          && ((const int *) addressof (c) == &c[0])))
98     __builtin_abort ();
100   return 0;