1 // LWG2296 - addressof should be constexpr
2 // { dg-do run { target c++11 } }
3
4 template <typename T>
5 constexpr inline T *
addressof(T & x)6 addressof (T &x) noexcept
7 {
8 return __builtin_addressof (x);
9 }
10
11 int i;
12 static_assert (__builtin_addressof (i) == &i, "");
13 static_assert (addressof (i) == &i, "");
14
15 constexpr int &j = i;
16 static_assert (__builtin_addressof (j) == &i, "");
17 static_assert (addressof (j) == &i, "");
18
19 struct S { int s; } s;
20 static_assert (__builtin_addressof (s) == &s, "");
21 static_assert (addressof (s) == &s, "");
22
23 struct T
24 {
25 static T tt;
TT26 constexpr T () : p (addressof (tt)) {}
27 constexpr T *operator & () const { return p; }
28 T *p;
29 };
30 constexpr T t;
31 T T::tt;
32 static_assert (&t == __builtin_addressof (T::tt), "");
33 static_assert (&t == addressof (T::tt), "");
34
35 struct S x, y;
36
37 constexpr S *
foo(bool b)38 foo (bool b)
39 {
40 return __builtin_addressof (b ? x : y);
41 }
42
43 constexpr S *
bar(bool b,S & c,S & d)44 bar (bool b, S &c, S &d)
45 {
46 return __builtin_addressof (b ? c : d);
47 }
48
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, "");
53
54 constexpr S *
foo2(bool b)55 foo2 (bool b)
56 {
57 return addressof (b ? x : y);
58 }
59
60 constexpr S *
bar2(bool b,S & c,S & d)61 bar2 (bool b, S &c, S &d)
62 {
63 return addressof (b ? c : d);
64 }
65
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, "");
70
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 };
75
76 void
baz()77 baz ()
78 {
79 }
80
81 int
main()82 main ()
83 {
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 ();
90
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 ();
99
100 return 0;
101 }
102