From d4c0d267624c92d2989a7155731c6fa9a9e71100 Mon Sep 17 00:00:00 2001 From: Brett Simmers Date: Wed, 8 Jun 2016 17:19:20 -0700 Subject: [PATCH] Allow const mapped types in folly::AtomicHash(Array|Map) Summary: Without this const_cast, it's impossible to insert anything into an AHA with a const mapped_type, making it effectively useless. Reviewed By: yfeldblum Differential Revision: D3405687 fbshipit-source-id: 3ecba19e0e92661c2c537c747b4927176104939f --- folly/AtomicHashArray-inl.h | 6 +++++- folly/test/AtomicHashArrayTest.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/folly/AtomicHashArray-inl.h b/folly/AtomicHashArray-inl.h index 84b01ef3..c6376822 100644 --- a/folly/AtomicHashArray-inl.h +++ b/folly/AtomicHashArray-inl.h @@ -152,7 +152,11 @@ insertInternal(LookupKeyT key_in, ArgTs&&... vCtorArgs) { checkLegalKeyIfKey(key_new); } DCHECK(relaxedLoadKey(*cell) == kLockedKey_); - new (&cell->second) ValueT(std::forward(vCtorArgs)...); + // A const mapped_type is only constant once constructed, so cast + // away any const for the placement new here. + using mapped = typename std::remove_const::type; + new (const_cast(&cell->second)) + ValueT(std::forward(vCtorArgs)...); unlockCell(cell, key_new); // Sets the new key } catch (...) { // Transition back to empty key---requires handling diff --git a/folly/test/AtomicHashArrayTest.cpp b/folly/test/AtomicHashArrayTest.cpp index 4facc7d7..92be30da 100644 --- a/folly/test/AtomicHashArrayTest.cpp +++ b/folly/test/AtomicHashArrayTest.cpp @@ -333,3 +333,10 @@ TEST(Aha, LookupAny) { free(it.first); } } + +using AHAIntCInt = AtomicHashArray; + +TEST(Aha, ConstValue) { + auto aha = AHAIntCInt::create(10); + aha->emplace(1, 2); +} -- 2.34.1