Skip to content

Commit

Permalink
Fix return type of insert_or_assign
Browse files Browse the repository at this point in the history
Summary: I misread the return types on cppreference; the overloads I implemented are supposed to return `std::pair<iterator, bool>`, not `iterator`. Whoops!

Reviewed By: yfeldblum

Differential Revision: D48375389

fbshipit-source-id: daf97a6db95c420b85dd7fcdafb84b0885bf1c9d
  • Loading branch information
swolchok authored and facebook-github-bot committed Sep 26, 2023
1 parent 6bd301a commit 7293c34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions folly/sorted_vector_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1219,21 +1219,21 @@ class sorted_vector_map : detail::growth_policy_wrapper<GrowthPolicy> {
}

template <typename M>
iterator insert_or_assign(const key_type& k, M&& obj) {
std::pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj) {
auto itAndInserted = try_emplace(k, std::forward<M>(obj));
if (!itAndInserted.second) {
itAndInserted.first->second = std::forward<M>(obj);
}
return itAndInserted.first;
return itAndInserted;
}

template <typename M>
iterator insert_or_assign(key_type&& k, M&& obj) {
std::pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj) {
auto itAndInserted = try_emplace(std::move(k), std::forward<M>(obj));
if (!itAndInserted.second) {
itAndInserted.first->second = std::forward<M>(obj);
}
return itAndInserted.first;
return itAndInserted;
}

size_type erase(const key_type& key) {
Expand Down
11 changes: 8 additions & 3 deletions folly/test/sorted_vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,9 @@ TEST(SortedVectorTypes, TestInsertOrAssign) {
{
auto k = folly::make_optional<int>(1);
auto v = folly::make_optional<std::string>("1");
const auto it = map.insert_or_assign(std::move(k), std::move(v));
const auto& [it, inserted] =
map.insert_or_assign(std::move(k), std::move(v));
EXPECT_TRUE(inserted);
EXPECT_EQ(it->first, 1);
EXPECT_EQ(it->second, "1");
EXPECT_FALSE(k);
Expand All @@ -1499,7 +1501,9 @@ TEST(SortedVectorTypes, TestInsertOrAssign) {
{
auto k = folly::make_optional<int>(1);
auto v = folly::make_optional<std::string>("another 1");
const auto it = map.insert_or_assign(std::move(k), std::move(v));
const auto& [it, inserted] =
map.insert_or_assign(std::move(k), std::move(v));
EXPECT_FALSE(inserted);
EXPECT_EQ(it->first, 1);
EXPECT_EQ(it->second, "another 1");
EXPECT_EQ(k, 1);
Expand All @@ -1509,7 +1513,8 @@ TEST(SortedVectorTypes, TestInsertOrAssign) {
{
auto k = folly::make_optional<int>(2);
auto v = folly::make_optional<std::string>("2");
const auto it = map.insert_or_assign(k, v);
const auto& [it, inserted] = map.insert_or_assign(k, v);
EXPECT_TRUE(inserted);
EXPECT_EQ(it->first, 2);
EXPECT_EQ(it->second, "2");
EXPECT_EQ(k, 2);
Expand Down

0 comments on commit 7293c34

Please sign in to comment.