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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150711/a6ad0882/attachment.html>
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
> 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? > > -- SanjoyJust 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.