From 857754a1cb73b3b71aef847ba8d7e983d4d77e2e Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Thu, 21 Jul 2016 13:37:53 +0000 Subject: [DenseMap] Add a C++17-style try_emplace method. This provides an elegant pattern to solve the "construct if not in map already" problem we have many times in LLVM. Without try_emplace we either have to rely on a sentinel value (nullptr) or do two lookups. llvm-svn: 276277 --- llvm/unittests/ADT/DenseMapTest.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'llvm/unittests/ADT/DenseMapTest.cpp') diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index db00f8cf8e5..cbf1a44fa60 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -619,4 +619,14 @@ TEST(DenseMapCustomTest, SmallDenseMapGrowTest) { EXPECT_TRUE(map.find(32) == map.end()); } +TEST(DenseMapCustomTest, TryEmplaceTest) { + DenseMap> Map; + std::unique_ptr P(new int(2)); + auto Try1 = Map.try_emplace(0, new int(1)); + EXPECT_TRUE(Try1.second); + auto Try2 = Map.try_emplace(0, std::move(P)); + EXPECT_FALSE(Try2.second); + EXPECT_EQ(Try1.first, Try2.first); + EXPECT_NE(nullptr, P); +} } -- cgit v1.2.3