[LLVMdev] StringMap question (original) (raw)

Duncan P. N. Exon Smith dexonsmith at apple.com
Mon Jul 13 10:24:28 PDT 2015


On 2015-Jul-11, at 12:03, Sanjoy Das <sanjoy at playingwithpointers.com> wrote:

On Sat, Jul 11, 2015 at 5:41 AM, Valery Pushkar <pollnossa at gmail.com> wrote: Hello everyone!

I'm a newcomer for the great LLVM project. I've started to explore the source code of LLVM project to become more familiar with it, and I've found some strange usage of move semantics in constructor of StringMapImpl(StringMapImpl &&RHS) {...} class in include/llvm/ADT/StringMap.h line 56. Could anyone explain me the purpose of zeroing of all fields of RHS in the body of this constructor if the declaration of the constructor uses move semantics? Here is the consturctor code below: I'm not a C++ expert, but this looks like a basic move-constructor'ism. We know that RHS is not going to be used after the move constructor has run, so we steal the memory (TheTable and related metadata) into *this. Zeroing out the fields ensures that when the destructor of RHS runs, it won't try to delete memory that *this will use from now on. Does that answer your question? -- Sanjoy

Just to add to this, the contract of move constructors/assignment is that, after copying/moving resources, the original should be left in a consistent state. If we didn't zero out RHS's fields, then when its destructor eventually ran, it'd delete the contents of this since it would still be pointing there.



More information about the llvm-dev mailing list