StringMap question

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:

StringMapImpl(StringMapImpl &&RHS)
: TheTable(RHS.TheTable), NumBuckets(RHS.NumBuckets),
NumItems(RHS.NumItems), NumTombstones(RHS.NumTombstones),
ItemSize(RHS.ItemSize) {
RHS.TheTable = nullptr;
RHS.NumBuckets = 0;
RHS.NumItems = 0;
RHS.NumTombstones = 0;
}

Thank you very much!

BR,
Valeriy

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.