diff options
author | Volodymyr Sapsai <vsapsai@apple.com> | 2019-01-08 00:03:16 +0000 |
---|---|---|
committer | Volodymyr Sapsai <vsapsai@apple.com> | 2019-01-08 00:03:16 +0000 |
commit | fb0e1908d4e94d83da2996a6f3fe188cc72c00c2 (patch) | |
tree | 4ee17a7c08b6afa5211fc719f3cd47f982779804 /libcxx/test | |
parent | 91b43adb69d91faa1546eb9df0ead7b550a855f2 (diff) |
[libcxx] Optimize vectors construction of trivial types from an iterator range with const-ness mismatch.
We already have a specialization that will use memcpy for construction
of trivial types from an iterator range like
std::vector<int>(int *, int *);
But if we have const-ness mismatch like
std::vector<int>(const int *, const int *);
we would use a slow path that copies each element individually. This change
enables the optimal specialization for const-ness mismatch. Fixes PR37574.
Contributions to the patch are made by Arthur O'Dwyer, Louis Dionne.
rdar://problem/40485845
Reviewers: mclow.lists, EricWF, ldionne, scanon
Reviewed By: ldionne
Subscribers: christof, ldionne, howard.hinnant, cfe-commits
Differential Revision: https://reviews.llvm.org/D48342
llvm-svn: 350583
Diffstat (limited to 'libcxx/test')
-rw-r--r-- | libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp index 21a7df4a7f03..60fe2899ac20 100644 --- a/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp @@ -146,9 +146,40 @@ void test_ctor_under_alloc() { #endif } +// Initialize a vector with a different value type. +void test_ctor_with_different_value_type() { + { + // Make sure initialization is performed with each element value, not with + // a memory blob. + float array[3] = {0.0f, 1.0f, 2.0f}; + std::vector<int> v(array, array + 3); + assert(v[0] == 0); + assert(v[1] == 1); + assert(v[2] == 2); + } + struct X { int x; }; + struct Y { int y; }; + struct Z : X, Y { int z; }; + { + Z z; + Z *array[1] = { &z }; + // Though the types Z* and Y* are very similar, initialization still cannot + // be done with `memcpy`. + std::vector<Y*> v(array, array + 1); + assert(v[0] == &z); + } + { + // Though the types are different, initialization can be done with `memcpy`. + int32_t array[1] = { -1 }; + std::vector<uint32_t> v(array, array + 1); + assert(v[0] == 4294967295); + } +} + int main() { basic_test_cases(); emplaceable_concept_tests(); // See PR34898 test_ctor_under_alloc(); + test_ctor_with_different_value_type(); } |