diff options
Diffstat (limited to 'absl/container')
-rw-r--r-- | absl/container/inlined_vector.h | 19 | ||||
-rw-r--r-- | absl/container/inlined_vector_test.cc | 13 |
2 files changed, 25 insertions, 7 deletions
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index e0bb900cbb66..4bf9c18adec4 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -365,13 +365,14 @@ class InlinedVector { // InlinedVector::emplace_back() // // Constructs and appends an object to the inlined vector. + // + // Returns a reference to the inserted element. template <typename... Args> - void emplace_back(Args&&... args) { + value_type& emplace_back(Args&&... args) { size_type s = size(); assert(s <= capacity()); if (ABSL_PREDICT_FALSE(s == capacity())) { - GrowAndEmplaceBack(std::forward<Args>(args)...); - return; + return GrowAndEmplaceBack(std::forward<Args>(args)...); } assert(s < capacity()); @@ -383,7 +384,7 @@ class InlinedVector { tag().set_inline_size(s + 1); space = inlined_space(); } - Construct(space + s, std::forward<Args>(args)...); + return Construct(space + s, std::forward<Args>(args)...); } // InlinedVector::push_back() @@ -703,26 +704,30 @@ class InlinedVector { } template <typename... Args> - void GrowAndEmplaceBack(Args&&... args) { + value_type& GrowAndEmplaceBack(Args&&... args) { assert(size() == capacity()); const size_type s = size(); Allocation new_allocation(allocator(), 2 * capacity()); - Construct(new_allocation.buffer() + s, std::forward<Args>(args)...); + value_type& new_element = + Construct(new_allocation.buffer() + s, std::forward<Args>(args)...); UninitializedCopy(std::make_move_iterator(data()), std::make_move_iterator(data() + s), new_allocation.buffer()); ResetAllocation(new_allocation, s + 1); + + return new_element; } void InitAssign(size_type n); void InitAssign(size_type n, const value_type& t); template <typename... Args> - void Construct(pointer p, Args&&... args) { + value_type& Construct(pointer p, Args&&... args) { AllocatorTraits::construct(allocator(), p, std::forward<Args>(args)...); + return *p; } template <typename Iter> diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index 055bca983cac..8527ba1aa008 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -393,6 +393,19 @@ TEST(InlinedVectorTest, Noexcept) { absl::InlinedVector<MoveCanThrow, 2>>::value)); } +TEST(InlinedVectorTest, EmplaceBack) { + absl::InlinedVector<std::pair<std::string, int>, 1> v; + + auto& inlined_element = v.emplace_back("answer", 42); + EXPECT_EQ(&inlined_element, &v[0]); + EXPECT_EQ(inlined_element.first, "answer"); + EXPECT_EQ(inlined_element.second, 42); + + auto& allocated_element = v.emplace_back("taxicab", 1729); + EXPECT_EQ(&allocated_element, &v[1]); + EXPECT_EQ(allocated_element.first, "taxicab"); + EXPECT_EQ(allocated_element.second, 1729); +} TEST(IntVec, Insert) { for (int len = 0; len < 20; len++) { |