Commit 5fc38975 authored by Joe Loser's avatar Joe Loser Committed by Facebook Github Bot

Return reference in FBVector::emplace_back (#1114)

Summary:
- Since C++17, `vector::emplace_back()` returns a reference to
  the new element inserted.
- Differential Revision: D15007059 added support for these semantics in
  `small_vector`. This adds it for `FBVector` too to be symmetric.
Pull Request resolved: https://github.com/facebook/folly/pull/1114

Reviewed By: ot

Differential Revision: D15053815

Pulled By: yfeldblum

fbshipit-source-id: 73ce9687e77ee9b97f12151cc148a84613b3ed1d
parent a79471ad
...@@ -1136,13 +1136,14 @@ class fbvector { ...@@ -1136,13 +1136,14 @@ class fbvector {
// modifiers (common) // modifiers (common)
public: public:
template <class... Args> template <class... Args>
void emplace_back(Args&&... args) { reference emplace_back(Args&&... args) {
if (impl_.e_ != impl_.z_) { if (impl_.e_ != impl_.z_) {
M_construct(impl_.e_, std::forward<Args>(args)...); M_construct(impl_.e_, std::forward<Args>(args)...);
++impl_.e_; ++impl_.e_;
} else { } else {
emplace_back_aux(std::forward<Args>(args)...); emplace_back_aux(std::forward<Args>(args)...);
} }
return back();
} }
void push_back(const T& value) { void push_back(const T& value) {
......
...@@ -124,8 +124,10 @@ TEST(fbvector, emplace) { ...@@ -124,8 +124,10 @@ TEST(fbvector, emplace) {
fbvector<std::string> s(12, "asd"); fbvector<std::string> s(12, "asd");
EXPECT_EQ(s.size(), 12); EXPECT_EQ(s.size(), 12);
EXPECT_EQ(s.front(), "asd"); EXPECT_EQ(s.front(), "asd");
s.emplace_back("funk"); const auto& emplaced = s.emplace_back("funk");
EXPECT_EQ(emplaced, "funk");
EXPECT_EQ(s.back(), "funk"); EXPECT_EQ(s.back(), "funk");
EXPECT_EQ(std::addressof(emplaced), std::addressof(s.back()));
} }
TEST(fbvector, initializer_lists) { TEST(fbvector, initializer_lists) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment