From 198a2a59ee8e99365c8d4ef6d5346e6743f58cb1 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Tue, 13 Aug 2013 23:54:12 +0000 Subject: [PATCH] Implement LWG Issue #2187 (emplace_back and emplace for vector) git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@188333 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/vector | 14 ++++ .../sequences/vector.bool/emplace.pass.cpp | 68 +++++++++++++++++++ .../vector.bool/emplace_back.pass.cpp | 57 ++++++++++++++++ www/cxx1y_status.html | 2 +- 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 test/containers/sequences/vector.bool/emplace.pass.cpp create mode 100644 test/containers/sequences/vector.bool/emplace_back.pass.cpp diff --git a/include/vector b/include/vector index df143445f..0855e8b93 100644 --- a/include/vector +++ b/include/vector @@ -216,8 +216,10 @@ public: const_reference back() const; void push_back(const value_type& x); + template void emplace_back(Args&&... args); // C++14 void pop_back(); + template iterator emplace(const_iterator position, Args&&... args); // C++14 iterator insert(const_iterator position, const value_type& x); iterator insert(const_iterator position, size_type n, const value_type& x); template @@ -2221,8 +2223,20 @@ public: _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);} void push_back(const value_type& __x); +#if _LIBCPP_STD_VER > 11 + template + _LIBCPP_INLINE_VISIBILITY void emplace_back(_Args&&... __args) + { push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); } +#endif + _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;} +#if _LIBCPP_STD_VER > 11 + template + _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args) + { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); } +#endif + iterator insert(const_iterator __position, const value_type& __x); iterator insert(const_iterator __position, size_type __n, const value_type& __x); iterator insert(const_iterator __position, size_type __n, const_reference __x); diff --git a/test/containers/sequences/vector.bool/emplace.pass.cpp b/test/containers/sequences/vector.bool/emplace.pass.cpp new file mode 100644 index 000000000..c57544085 --- /dev/null +++ b/test/containers/sequences/vector.bool/emplace.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// +// vector + +// template iterator emplace(const_iterator pos, Args&&... args); + +#include +#include +#include "../../min_allocator.h" + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + typedef std::vector C; + C c; + + C::iterator i = c.emplace(c.cbegin()); + assert(i == c.begin()); + assert(c.size() == 1); + assert(c.front() == false); + + i = c.emplace(c.cend(), true); + assert(i == c.end()-1); + assert(c.size() == 2); + assert(c.front() == false); + assert(c.back() == true); + + i = c.emplace(c.cbegin()+1, 1 == 1); + assert(i == c.begin()+1); + assert(c.size() == 3); + assert(c.front() == false); + assert(c[1] == true); + assert(c.back() == true); + } + { + typedef std::vector> C; + C c; + + C::iterator i = c.emplace(c.cbegin()); + assert(i == c.begin()); + assert(c.size() == 1); + assert(c.front() == false); + + i = c.emplace(c.cend(), true); + assert(i == c.end()-1); + assert(c.size() == 2); + assert(c.front() == false); + assert(c.back() == true); + + i = c.emplace(c.cbegin()+1, 1 == 1); + assert(i == c.begin()+1); + assert(c.size() == 3); + assert(c.size() == 3); + assert(c.front() == false); + assert(c[1] == true); + assert(c.back() == true); + } +#endif +} diff --git a/test/containers/sequences/vector.bool/emplace_back.pass.cpp b/test/containers/sequences/vector.bool/emplace_back.pass.cpp new file mode 100644 index 000000000..86e0d7a8d --- /dev/null +++ b/test/containers/sequences/vector.bool/emplace_back.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// +// vector.bool + +// template void emplace_back(Args&&... args); + +#include +#include +#include "../../min_allocator.h" + + +int main() +{ +#if _LIBCPP_STD_VER > 11 + { + typedef std::vector C; + C c; + c.emplace_back(); + assert(c.size() == 1); + assert(c.front() == false); + c.emplace_back(true); + assert(c.size() == 2); + assert(c.front() == false); + assert(c.back() == true); + c.emplace_back(1 == 1); + assert(c.size() == 3); + assert(c.front() == false); + assert(c[1] == true); + assert(c.back() == true); + } + { + typedef std::vector> C; + C c; + + c.emplace_back(); + assert(c.size() == 1); + assert(c.front() == false); + c.emplace_back(true); + assert(c.size() == 2); + assert(c.front() == false); + assert(c.back() == true); + c.emplace_back(1 == 1); + assert(c.size() == 3); + assert(c.front() == false); + assert(c[1] == true); + assert(c.back() == true); + } +#endif +} diff --git a/www/cxx1y_status.html b/www/cxx1y_status.html index 031934fe3..ae55f2317 100644 --- a/www/cxx1y_status.html +++ b/www/cxx1y_status.html @@ -123,7 +123,7 @@ 2174wstring_convert::converted() should be noexceptBristol 2175string_convert and wbuffer_convert validityBristol 2177Requirements on Copy/MoveInsertableBristol - 2187vector is missing emplace and emplace_back member functionsBristolNot complete + 2187vector<bool> is missing emplace and emplace_back member functionsBristolComplete 2197Specification of is_[un]signed unclear for non-arithmetic typesBristolComplete 2200Data race avoidance for all containers, not only for sequencesBristol 2209assign() overspecified for sequence containersBristol