| 1 |
// PR c++/118923 |
| 2 |
// { dg-do run { target c++11 } } |
| 3 |
// { dg-additional-options "-frange-for-ext-temps" { target c++23 } } |
| 4 |
// { dg-additional-options "-fno-range-for-ext-temps" { target c++20_down } } |
| 5 |
|
| 6 |
int g; |
| 7 |
|
| 8 |
struct A { |
| 9 |
int a[3]; |
| 10 |
A (int x, int y, int z) : a{x, y, z} { if ((g++ & 7) != 4) __builtin_abort (); } |
| 11 |
A (const A &x) = delete; |
| 12 |
~A () { if ((g++ & 7) != 7 - 2 * (__cpp_range_based_for >= 202211)) __builtin_abort (); } |
| 13 |
int *begin () { return a; } |
| 14 |
int *end () { return a + 3; } |
| 15 |
}; |
| 16 |
|
| 17 |
struct B { |
| 18 |
B () { if ((g++ & 7) != 3) __builtin_abort (); } |
| 19 |
B (const B &) = delete; |
| 20 |
~B () { if ((g++ & 7) != 5 + (__cpp_range_based_for >= 202211)) __builtin_abort (); } |
| 21 |
}; |
| 22 |
|
| 23 |
struct C { |
| 24 |
A foo (const B &) { return { 1, 2, 3 }; } |
| 25 |
A bar (const B &) { return { 4, 5, 6 }; } |
| 26 |
bool baz () { return b; } |
| 27 |
bool b = false; |
| 28 |
static C c; |
| 29 |
}; |
| 30 |
|
| 31 |
C C::c; |
| 32 |
|
| 33 |
struct D { |
| 34 |
D () { if ((g++ & 5) != 0) __builtin_abort (); } |
| 35 |
D (const D &) = delete; |
| 36 |
~D () { if ((g & 7) != 1 && (g & 7) != 6 + (__cpp_range_based_for >= 202211)) __builtin_abort (); g++; } |
| 37 |
}; |
| 38 |
|
| 39 |
inline C * |
| 40 |
qux (const D &) |
| 41 |
{ |
| 42 |
return &C::c; |
| 43 |
} |
| 44 |
|
| 45 |
void |
| 46 |
foo () |
| 47 |
{ |
| 48 |
int z = 1; |
| 49 |
auto d = qux (D {})->baz () ? &C::bar : &C::foo; |
| 50 |
for (const int &r : (qux (D {})->*d) (B {})) |
| 51 |
if (z++ != r) |
| 52 |
__builtin_abort (); |
| 53 |
C::c.b = true; |
| 54 |
d = qux (D {})->baz () ? &C::bar : &C::foo; |
| 55 |
for (const int &r : (qux (D {})->*d) (B {})) |
| 56 |
if (z++ != r) |
| 57 |
__builtin_abort (); |
| 58 |
} |
| 59 |
|
| 60 |
int |
| 61 |
main () |
| 62 |
{ |
| 63 |
foo (); |
| 64 |
if (g != 16) |
| 65 |
__builtin_abort (); |
| 66 |
} |