Skip to content

Commit 194efde

Browse files
authored
Fix ranges::uninitialized_copy/uninitialized_move for trivial types (#1713)
1 parent ba47118 commit 194efde

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

stl/inc/memory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace ranges {
7878
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_OSe, _Out>);
7979
_STL_INTERNAL_STATIC_ASSERT(constructible_from<iter_value_t<_Out>, iter_reference_t<_It>>);
8080

81-
if constexpr (is_same_v<_Se, _It> && _Ptr_copy_cat<_It, _Out>::_Really_trivial) {
81+
if constexpr (is_same_v<_Se, _It> && is_same_v<_OSe, _Out> && _Ptr_copy_cat<_It, _Out>::_Really_trivial) {
8282
return _Copy_memcpy_common(_IFirst, _ILast, _OFirst, _OLast);
8383
} else {
8484
_Uninitialized_backout _Backout{_STD move(_OFirst)};

stl/inc/xmemory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ namespace ranges {
15821582
uninitialized_move_result<_It, _Out> _Uninitialized_move_unchecked(
15831583
_It _IFirst, const _Se _ILast, _Out _OFirst, const _OSe _OLast) {
15841584
// clang-format on
1585-
if constexpr (is_same_v<_Se, _It> && _Ptr_move_cat<_It, _Out>::_Really_trivial) {
1585+
if constexpr (is_same_v<_Se, _It> && is_same_v<_OSe, _Out> && _Ptr_move_cat<_It, _Out>::_Really_trivial) {
15861586
return _Copy_memcpy_common(_IFirst, _ILast, _OFirst, _OLast);
15871587
} else {
15881588
_Uninitialized_backout _Backout{_STD move(_OFirst)};

tests/std/tests/P0896R4_ranges_alg_uninitialized_copy/test.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct memcpy_test {
203203
static constexpr int expected_input_long[] = {13, 55, 12345, 42};
204204

205205
static void call() {
206-
{ // Validate only range overload
206+
{ // Validate matching ranges
207207
int input[] = {13, 55, 12345};
208208
int output[] = {-1, -1, -1};
209209

@@ -235,6 +235,30 @@ struct memcpy_test {
235235
assert(ranges::equal(input, expected_input_long));
236236
assert(ranges::equal(output, expected_output));
237237
}
238+
239+
{ // Validate non-common input range
240+
int input[] = {13, 55, 12345};
241+
int output[] = {-1, -1, -1};
242+
243+
const auto result =
244+
ranges::uninitialized_copy(begin(input), unreachable_sentinel, begin(output), end(output));
245+
assert(result.in == end(input));
246+
assert(result.out == end(output));
247+
assert(ranges::equal(input, expected_input));
248+
assert(ranges::equal(output, expected_output));
249+
}
250+
251+
{ // Validate non-common output range
252+
int input[] = {13, 55, 12345};
253+
int output[] = {-1, -1, -1};
254+
255+
const auto result =
256+
ranges::uninitialized_copy(begin(input), end(input), begin(output), unreachable_sentinel);
257+
assert(result.in == end(input));
258+
assert(result.out == end(output));
259+
assert(ranges::equal(input, expected_input));
260+
assert(ranges::equal(output, expected_output));
261+
}
238262
}
239263
};
240264

tests/std/tests/P0896R4_ranges_alg_uninitialized_move/test.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ struct memcpy_test {
204204
static constexpr int expected_input_long[] = {13, 55, 12345, 42};
205205

206206
static void call() {
207-
{ // Validate range
207+
{ // Validate matching ranges
208208
int input[] = {13, 55, 12345};
209209
int output[] = {-1, -1, -1};
210210

211-
ranges::uninitialized_move(input, output);
211+
const auto result = ranges::uninitialized_move(input, output);
212+
assert(result.in == end(input));
213+
assert(result.out == end(output));
212214
assert(ranges::equal(input, expected_input));
213215
assert(ranges::equal(output, expected_output));
214216
}
@@ -234,6 +236,30 @@ struct memcpy_test {
234236
assert(ranges::equal(input, expected_input_long));
235237
assert(ranges::equal(output, expected_output));
236238
}
239+
240+
{ // Validate non-common input range
241+
int input[] = {13, 55, 12345};
242+
int output[] = {-1, -1, -1};
243+
244+
const auto result =
245+
ranges::uninitialized_move(begin(input), unreachable_sentinel, begin(output), end(output));
246+
assert(result.in == end(input));
247+
assert(result.out == end(output));
248+
assert(ranges::equal(input, expected_input));
249+
assert(ranges::equal(output, expected_output));
250+
}
251+
252+
{ // Validate non-common output range
253+
int input[] = {13, 55, 12345};
254+
int output[] = {-1, -1, -1};
255+
256+
const auto result =
257+
ranges::uninitialized_move(begin(input), end(input), begin(output), unreachable_sentinel);
258+
assert(result.in == end(input));
259+
assert(result.out == end(output));
260+
assert(ranges::equal(input, expected_input));
261+
assert(ranges::equal(output, expected_output));
262+
}
237263
}
238264
};
239265

0 commit comments

Comments
 (0)