]> gcc.gnu.org Git - gcc.git/blame - gcc/testsuite/g++.dg/cpp0x/addressof1.C
PR c++/92590 - wrong handling of inherited default ctor.
[gcc.git] / gcc / testsuite / g++.dg / cpp0x / addressof1.C
CommitLineData
be845b04
JJ
1// LWG2296 - addressof should be constexpr
2// { dg-do run { target c++11 } }
3
4template <typename T>
5constexpr inline T *
6addressof (T &x) noexcept
7{
8 return __builtin_addressof (x);
9}
10
11int i;
12static_assert (__builtin_addressof (i) == &i, "");
13static_assert (addressof (i) == &i, "");
14
15constexpr int &j = i;
16static_assert (__builtin_addressof (j) == &i, "");
17static_assert (addressof (j) == &i, "");
18
19struct S { int s; } s;
20static_assert (__builtin_addressof (s) == &s, "");
be845b04 21static_assert (addressof (s) == &s, "");
be845b04
JJ
22
23struct T
24{
25 static T tt;
26 constexpr T () : p (addressof (tt)) {}
27 constexpr T *operator & () const { return p; }
28 T *p;
29};
30constexpr T t;
31T T::tt;
be845b04 32static_assert (&t == __builtin_addressof (T::tt), "");
be845b04
JJ
33static_assert (&t == addressof (T::tt), "");
34
35struct S x, y;
36
37constexpr S *
38foo (bool b)
39{
40 return __builtin_addressof (b ? x : y);
41}
42
43constexpr S *
44bar (bool b, S &c, S &d)
45{
46 return __builtin_addressof (b ? c : d);
47}
48
49static_assert (foo (false) == &y, "");
50static_assert (foo (true) == &x, "");
51static_assert (bar (false, y, x) == &x, "");
52static_assert (bar (true, y, x) == &y, "");
53
54constexpr S *
55foo2 (bool b)
56{
57 return addressof (b ? x : y);
58}
59
60constexpr S *
61bar2 (bool b, S &c, S &d)
62{
63 return addressof (b ? c : d);
64}
65
66static_assert (foo2 (false) == &y, "");
67static_assert (foo2 (true) == &x, "");
68static_assert (bar2 (false, y, x) == &x, "");
69static_assert (bar2 (true, y, x) == &y, "");
70
71constexpr int a = 1;
72static_assert (__builtin_addressof (a) == &a, "");
73static_assert (addressof (a) == &a, "");
74constexpr int c[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
be845b04
JJ
75
76void
77baz ()
78{
79}
80
81int
82main ()
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 ();
7d75ea04
JJ
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;
be845b04 101}
This page took 2.41246 seconds and 5 git commands to generate.