| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
#include <algorithm> |
| 4 |
#include <testsuite_hooks.h> |
| 5 |
|
| 6 |
// PR libstdc++/108846 std::copy, std::copy_n and std::copy_backward |
| 7 |
// on potentially overlapping subobjects |
| 8 |
|
| 9 |
struct B { |
| 10 |
B(int i, short j) : i(i), j(j) {} |
| 11 |
int i; |
| 12 |
short j; |
| 13 |
}; |
| 14 |
struct D : B { |
| 15 |
D(int i, short j, short x) : B(i, j), x(x) {} |
| 16 |
short x; // Stored in tail padding of B |
| 17 |
}; |
| 18 |
|
| 19 |
void |
| 20 |
test_pr108846() |
| 21 |
{ |
| 22 |
D ddst(1, 2, 3); |
| 23 |
D dsrc(4, 5, 6); |
| 24 |
B *dst = &ddst; |
| 25 |
B *src = &dsrc; |
| 26 |
// If this is optimized to memmove it will overwrite tail padding. |
| 27 |
std::move(src, src+1, dst); |
| 28 |
VERIFY(ddst.x == 3); |
| 29 |
#if __cpp_lib_ranges >= 201911L |
| 30 |
std::ranges::move(src, src+1, dst); |
| 31 |
VERIFY(ddst.x == 3); |
| 32 |
#endif |
| 33 |
} |
| 34 |
|
| 35 |
struct B2 { |
| 36 |
B2(int i, short j) : i(i), j(j) {} |
| 37 |
B2& operator=(B2&& b) { i = b.i; j = b.j; return *this; } |
| 38 |
int i; |
| 39 |
short j; |
| 40 |
}; |
| 41 |
struct D2 : B2 { |
| 42 |
D2(int i, short j, short x) : B2(i, j), x(x) {} |
| 43 |
short x; // Stored in tail padding of B2 |
| 44 |
}; |
| 45 |
|
| 46 |
void |
| 47 |
test_move_only() |
| 48 |
{ |
| 49 |
D2 ddst(1, 2, 3); |
| 50 |
D2 dsrc(4, 5, 6); |
| 51 |
B2 *dst = &ddst; |
| 52 |
B2 *src = &dsrc; |
| 53 |
// Ensure the not-taken trivial copy path works for this type. |
| 54 |
std::move(src, src+1, dst); |
| 55 |
VERIFY(ddst.x == 3); |
| 56 |
#if __cpp_lib_ranges >= 201911L |
| 57 |
std::ranges::move(src, src+1, dst); |
| 58 |
VERIFY(ddst.x == 3); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
struct B3 { |
| 63 |
B3(int i, short j) : i(i), j(j) {} |
| 64 |
B3& operator=(B3&&) = default; |
| 65 |
int i; |
| 66 |
short j; |
| 67 |
}; |
| 68 |
struct D3 : B3 { |
| 69 |
D3(int i, short j, short x) : B3(i, j), x(x) {} |
| 70 |
short x; // Stored in tail padding of B3 |
| 71 |
}; |
| 72 |
|
| 73 |
void |
| 74 |
test_move_only_trivial() |
| 75 |
{ |
| 76 |
D3 ddst(1, 2, 3); |
| 77 |
D3 dsrc(4, 5, 6); |
| 78 |
B3 *dst = &ddst; |
| 79 |
B3 *src = &dsrc; |
| 80 |
// If this is optimized to memmove it will overwrite tail padding. |
| 81 |
std::move(src, src+1, dst); |
| 82 |
VERIFY(ddst.x == 3); |
| 83 |
#if __cpp_lib_ranges >= 201911L |
| 84 |
std::ranges::move(src, src+1, dst); |
| 85 |
VERIFY(ddst.x == 3); |
| 86 |
#endif |
| 87 |
} |
| 88 |
|
| 89 |
int main() |
| 90 |
{ |
| 91 |
test_pr108846(); |
| 92 |
test_move_only(); |
| 93 |
test_move_only_trivial(); |
| 94 |
} |