From: Brett Simmers Date: Thu, 9 Jun 2016 00:19:20 +0000 (-0700) Subject: Allow const mapped types in folly::AtomicHash(Array|Map) X-Git-Tag: 2016.07.26~156 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=d4c0d267624c92d2989a7155731c6fa9a9e71100;p=folly.git 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 --- 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); +}