update try_emplace_l so that the lambda is called only when the key was already present - issue #60

This commit is contained in:
greg
2020-08-16 18:44:12 -04:00
parent 0d229b1159
commit 5a74cb42ff
2 changed files with 9 additions and 7 deletions
+4 -2
View File
@@ -3457,8 +3457,10 @@ public:
inner->set_.emplace_at(std::get<1>(res), std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...));
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
std::forward<F>(f)(Policy::value(&*it));
else {
auto it = this->iterator_at(inner, inner->set_.iterator_at(std::get<1>(res)));
std::forward<F>(f)(Policy::value(&*it));
}
return std::get<2>(res);
}
+5 -5
View File
@@ -32,13 +32,13 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) {
m.try_emplace_l(2, [](int& v) { v = 5; });
EXPECT_EQ(m[2], 5);
// insert a valye that is not already present
m.try_emplace_l(3, [](int& v) { assert(v == 0); v = 6; });
EXPECT_EQ(m[3], 6);
// insert a valye that is not already present. Will be default initialised to 0 and lambda not called
m.try_emplace_l(3, [](int& v) { assert(v == 0); /* should not be called when value constructed */ v = 6; });
EXPECT_EQ(m[3], 0);
// insert a valye that is not already present, provide argument to value-construct it
m.try_emplace_l(4, [](int& v) { assert(v == 999); v = 5; }, 999);
EXPECT_EQ(m[4], 5);
m.try_emplace_l(4, [](int& ) { assert(0); /* should not be called when value constructed */ }, 999);
EXPECT_EQ(m[4], 999);
}