Index: include/vector =================================================================== --- include/vector +++ include/vector @@ -674,6 +674,17 @@ const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(this->__begin_);} +#ifdef _LIBCPP_CXX03_LANG + _LIBCPP_INLINE_VISIBILITY + void __emplace_back(const value_type& __x) { push_back(__x); } +#else + template + _LIBCPP_INLINE_VISIBILITY + void __emplace_back(_Arg&& __arg) { + emplace_back(_VSTD::forward<_Arg>(__arg)); + } +#endif + _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x); #ifndef _LIBCPP_CXX03_LANG @@ -1128,7 +1139,7 @@ __get_db()->__insert_c(this); #endif for (; __first != __last; ++__first) - push_back(*__first); + __emplace_back(*__first); } template @@ -1145,7 +1156,7 @@ __get_db()->__insert_c(this); #endif for (; __first != __last; ++__first) - push_back(*__first); + __emplace_back(*__first); } template @@ -1365,7 +1376,7 @@ { clear(); for (; __first != __last; ++__first) - push_back(*__first); + __emplace_back(*__first); } template Index: test/std/containers/sequences/vector/emplace_constructible.h =================================================================== --- /dev/null +++ test/std/containers/sequences/vector/emplace_constructible.h @@ -0,0 +1,71 @@ +#ifndef EMPLACE_CONSTRUCTIBLE_H +#define EMPLACE_CONSTRUCTIBLE_H + +#include "test_macros.h" + +#if TEST_STD_VER >= 11 + template + struct EmplaceConstructible { + T value; + explicit EmplaceConstructible(T value) : value(value) {} + EmplaceConstructible(EmplaceConstructible const&) = delete; + }; + + template + struct EmplaceConstructibleAndMoveInsertable { + int copied = 0; + T value; + explicit EmplaceConstructibleAndMoveInsertable(T value) : value(value) {} + + EmplaceConstructibleAndMoveInsertable(EmplaceConstructibleAndMoveInsertable&& Other) + : copied(Other.copied + 1), value(std::move(Other.value)) + {} + }; + + template + struct EmplaceConstructibleAndMoveable { + int copied = 0; + int assigned = 0; + T value; + explicit EmplaceConstructibleAndMoveable(T value) noexcept : value(value) {} + + EmplaceConstructibleAndMoveable(EmplaceConstructibleAndMoveable&& Other) noexcept + : copied(Other.copied + 1), value(std::move(Other.value)) + {} + + EmplaceConstructibleAndMoveable& operator=(EmplaceConstructibleAndMoveable&& Other) noexcept { + copied = Other.copied; + assigned = Other.assigned + 1; + value = std::move(Other.value); + return *this; + } + }; + +template + struct EmplaceConstructibleMoveableAndAssignable { + int copied = 0; + int assigned = 0; + T value; + explicit EmplaceConstructibleMoveableAndAssignable(T value) noexcept : value(value) {} + + EmplaceConstructibleMoveableAndAssignable(EmplaceConstructibleMoveableAndAssignable&& Other) noexcept + : copied(Other.copied + 1), value(std::move(Other.value)) + {} + + EmplaceConstructibleMoveableAndAssignable& operator=(EmplaceConstructibleMoveableAndAssignable&& Other) noexcept { + copied = Other.copied; + assigned = Other.assigned + 1; + value = std::move(Other.value); + return *this; + } + + EmplaceConstructibleMoveableAndAssignable& operator=(T xvalue) { + value = std::move(xvalue); + ++assigned; + return *this; + } + }; +#endif + + +#endif // EMPLACE_CONSTRUCTIBLE_H Index: test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp =================================================================== --- /dev/null +++ test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// void assign(size_type n, const_reference v); + +#include +#include +#include +#include +#include "test_macros.h" +#include "min_allocator.h" +#include "asan_testing.h" +#include "test_iterators.h" +#if TEST_STD_VER >= 11 +#include "../emplace_constructible.h" +#endif + + +void test_emplaceable_concept() { +#if TEST_STD_VER >= 11 + int arr1[] = {42}; + int arr2[] = {1, 101, 42}; + { + using T = EmplaceConstructibleMoveableAndAssignable; + using It = forward_iterator; + { + std::vector v; + v.assign(It(arr1), It(std::end(arr1))); + assert(v[0].value == 42); + } + { + std::vector v; + v.assign(It(arr2), It(std::end(arr2))); + assert(v[0].value == 1); + assert(v[1].value == 101); + assert(v[2].value == 42); + } + } + { + using T = EmplaceConstructibleMoveableAndAssignable; + using It = input_iterator; + { + std::vector v; + v.assign(It(arr1), It(std::end(arr1))); + assert(v[0].copied == 0); + assert(v[0].value == 42); + } + { + std::vector v; + v.assign(It(arr2), It(std::end(arr2))); + //assert(v[0].copied == 0); + assert(v[0].value == 1); + //assert(v[1].copied == 0); + assert(v[1].value == 101); + assert(v[2].copied == 0); + assert(v[2].value == 42); + } + } +#endif +} + +int main() +{ + test_emplaceable_concept(); +} Index: test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp =================================================================== --- test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp +++ test/std/containers/sequences/vector/vector.cons/construct_iter_iter.pass.cpp @@ -20,40 +20,101 @@ #include "test_allocator.h" #include "min_allocator.h" #include "asan_testing.h" +#if TEST_STD_VER >= 11 +#include "../emplace_constructible.h" +#endif template -void -test(Iterator first, Iterator last) -{ - C c(first, last); - LIBCPP_ASSERT(c.__invariants()); - assert(c.size() == static_cast(std::distance(first, last))); - LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); - for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first) - assert(*i == *first); +void test(Iterator first, Iterator last) { + C c(first, last); + LIBCPP_ASSERT(c.__invariants()); + assert(c.size() == static_cast(std::distance(first, last))); + LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); + for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; + ++i, ++first) + assert(*i == *first); } -int main() -{ - int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; - int* an = a + sizeof(a)/sizeof(a[0]); - test >(input_iterator(a), input_iterator(an)); - test >(forward_iterator(a), forward_iterator(an)); - test >(bidirectional_iterator(a), bidirectional_iterator(an)); - test >(random_access_iterator(a), random_access_iterator(an)); - test >(a, an); - - test > >(input_iterator(a), input_iterator(an)); - // Add 1 for implementations that dynamically allocate a container proxy. - test > >(forward_iterator(a), forward_iterator(an)); - test > >(bidirectional_iterator(a), bidirectional_iterator(an)); - test > >(random_access_iterator(a), random_access_iterator(an)); - test > >(a, an); +static void basic_test_cases() { + int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; + int* an = a + sizeof(a) / sizeof(a[0]); + test >(input_iterator(a), + input_iterator(an)); + test >(forward_iterator(a), + forward_iterator(an)); + test >(bidirectional_iterator(a), + bidirectional_iterator(an)); + test >(random_access_iterator(a), + random_access_iterator(an)); + test >(a, an); + + test > >( + input_iterator(a), input_iterator(an)); + // Add 1 for implementations that dynamically allocate a container proxy. + test > >( + forward_iterator(a), forward_iterator(an)); + test > >( + bidirectional_iterator(a), + bidirectional_iterator(an)); + test > >( + random_access_iterator(a), + random_access_iterator(an)); + test > >(a, an); #if TEST_STD_VER >= 11 - test> >(input_iterator(a), input_iterator(an)); - test> >(forward_iterator(a), forward_iterator(an)); - test> >(bidirectional_iterator(a), bidirectional_iterator(an)); - test> >(random_access_iterator(a), random_access_iterator(an)); - test >(a, an); + test > >(input_iterator(a), + input_iterator(an)); + test > >( + forward_iterator(a), forward_iterator(an)); + test > >( + bidirectional_iterator(a), + bidirectional_iterator(an)); + test > >( + random_access_iterator(a), + random_access_iterator(an)); + test >(a, an); #endif } + +void emplaceable_concept_tests() { +#if TEST_STD_VER >= 11 + int arr1[] = {42}; + int arr2[] = {1, 101, 42}; + { + using T = EmplaceConstructible; + using It = forward_iterator; + { + std::vector v(It(arr1), It(std::end(arr1))); + assert(v[0].value == 42); + } + { + std::vector v(It(arr2), It(std::end(arr2))); + assert(v[0].value == 1); + assert(v[1].value == 101); + assert(v[2].value == 42); + } + } + { + using T = EmplaceConstructibleAndMoveInsertable; + using It = input_iterator; + { + std::vector v(It(arr1), It(std::end(arr1))); + assert(v[0].copied == 0); + assert(v[0].value == 42); + } + { + std::vector v(It(arr2), It(std::end(arr2))); + //assert(v[0].copied == 0); + assert(v[0].value == 1); + //assert(v[1].copied == 0); + assert(v[1].value == 101); + assert(v[2].copied == 0); + assert(v[2].value == 42); + } + } +#endif +} + +int main() { + basic_test_cases(); + emplaceable_concept_tests(); // See PR34898 +} Index: test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp =================================================================== --- test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp +++ test/std/containers/sequences/vector/vector.cons/construct_iter_iter_alloc.pass.cpp @@ -21,56 +21,113 @@ #include "test_allocator.h" #include "min_allocator.h" #include "asan_testing.h" +#if TEST_STD_VER >= 11 +#include "../emplace_constructible.h" +#endif template -void -test(Iterator first, Iterator last, const A& a) -{ - C c(first, last, a); - LIBCPP_ASSERT(c.__invariants()); - assert(c.size() == static_cast(std::distance(first, last))); - LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); - for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i, ++first) - assert(*i == *first); +void test(Iterator first, Iterator last, const A& a) { + C c(first, last, a); + LIBCPP_ASSERT(c.__invariants()); + assert(c.size() == static_cast(std::distance(first, last))); + LIBCPP_ASSERT(is_contiguous_container_asan_correct(c)); + for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; + ++i, ++first) + assert(*i == *first); } #if TEST_STD_VER >= 11 template -struct implicit_conv_allocator : min_allocator -{ - implicit_conv_allocator(void*) {} - implicit_conv_allocator(const implicit_conv_allocator&) = default; +struct implicit_conv_allocator : min_allocator { + implicit_conv_allocator(void*) {} + implicit_conv_allocator(const implicit_conv_allocator&) = default; - template - implicit_conv_allocator(implicit_conv_allocator) {} + template + implicit_conv_allocator(implicit_conv_allocator) {} }; #endif -int main() -{ - { +void basic_tests() { + { int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; - int* an = a + sizeof(a)/sizeof(a[0]); + int* an = a + sizeof(a) / sizeof(a[0]); std::allocator alloc; - test >(input_iterator(a), input_iterator(an), alloc); - test >(forward_iterator(a), forward_iterator(an), alloc); - test >(bidirectional_iterator(a), bidirectional_iterator(an), alloc); - test >(random_access_iterator(a), random_access_iterator(an), alloc); + test >(input_iterator(a), + input_iterator(an), alloc); + test >(forward_iterator(a), + forward_iterator(an), alloc); + test >(bidirectional_iterator(a), + bidirectional_iterator(an), alloc); + test >(random_access_iterator(a), + random_access_iterator(an), alloc); test >(a, an, alloc); - } + } #if TEST_STD_VER >= 11 - { + { int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 1, 0}; - int* an = a + sizeof(a)/sizeof(a[0]); + int* an = a + sizeof(a) / sizeof(a[0]); min_allocator alloc; - test> >(input_iterator(a), input_iterator(an), alloc); - test> >(forward_iterator(a), forward_iterator(an), alloc); - test> >(bidirectional_iterator(a), bidirectional_iterator(an), alloc); - test> >(random_access_iterator(a), random_access_iterator(an), alloc); - test> >(a, an, alloc); - test> >(a, an, nullptr); + test > >( + input_iterator(a), input_iterator(an), alloc); + test > >( + forward_iterator(a), forward_iterator(an), + alloc); + test > >( + bidirectional_iterator(a), + bidirectional_iterator(an), alloc); + test > >( + random_access_iterator(a), + random_access_iterator(an), alloc); + test > >(a, an, alloc); + test > >(a, an, nullptr); + } +#endif +} + +void emplaceable_concept_tests() { +#if TEST_STD_VER >= 11 + int arr1[] = {42}; + int arr2[] = {1, 101, 42}; + { + using T = EmplaceConstructible; + using It = forward_iterator; + using Alloc = std::allocator; + Alloc a; + { + std::vector v(It(arr1), It(std::end(arr1)), a); + assert(v[0].value == 42); + } + { + std::vector v(It(arr2), It(std::end(arr2)), a); + assert(v[0].value == 1); + assert(v[1].value == 101); + assert(v[2].value == 42); + } + } + { + using T = EmplaceConstructibleAndMoveInsertable; + using It = input_iterator; + using Alloc = std::allocator; + Alloc a; + { + std::vector v(It(arr1), It(std::end(arr1)), a); + assert(v[0].copied == 0); + assert(v[0].value == 42); } + { + std::vector v(It(arr2), It(std::end(arr2)), a); + assert(v[0].value == 1); + assert(v[1].value == 101); + assert(v[2].copied == 0); + assert(v[2].value == 42); + } + } #endif } + +int main() { + basic_tests(); + emplaceable_concept_tests(); // See PR34898 +}