| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
#define assert(X) do { if (!(X)) __builtin_abort(); } while (0) |
| 4 |
|
| 5 |
namespace std { |
| 6 |
template<typename T> struct tuple_size; |
| 7 |
template<int, typename> struct tuple_element; |
| 8 |
} |
| 9 |
|
| 10 |
struct A { |
| 11 |
int i; |
| 12 |
template <int I> int& get() { return i; } |
| 13 |
}; |
| 14 |
|
| 15 |
template<> struct std::tuple_size<A> { static const int value = 2; }; |
| 16 |
template<int I> struct std::tuple_element<I,A> { using type = int; }; |
| 17 |
|
| 18 |
struct B { |
| 19 |
int i; |
| 20 |
}; |
| 21 |
template <int I> int& get(B&& b) { return b.i; } |
| 22 |
template <int I> int& get(B& b) { return b.i; } |
| 23 |
|
| 24 |
template<> struct std::tuple_size<B> { static const int value = 2; }; |
| 25 |
template<int I> struct std::tuple_element<I,B> { using type = int; }; |
| 26 |
|
| 27 |
int main() |
| 28 |
{ |
| 29 |
{ |
| 30 |
A a = { 42 }; |
| 31 |
auto& [ x, y ] = a; |
| 32 |
assert (&x == &y && &x == &a.i && x == 42); |
| 33 |
|
| 34 |
auto [ x2, y2 ] = a; |
| 35 |
assert (&x2 == &y2 && &x2 != &a.i && x2 == 42); |
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
B b = { 42 }; |
| 40 |
auto& [ x, y ] = b; |
| 41 |
assert (&x == &y && &x == &b.i && x == 42); |
| 42 |
|
| 43 |
auto [ x2, y2 ] = b; |
| 44 |
assert (&x2 == &y2 && &x2 != &b.i && x2 == 42); |
| 45 |
} |
| 46 |
} |