[Development] What's the status of a moved-from object?

Thiago Macieira thiago.macieira at intel.com
Sun May 19 18:54:08 CEST 2019


On Sunday, 19 May 2019 05:24:14 PDT Giuseppe D'Angelo via Development wrote:
> Hence, I'll ask here: what should the status of a moved-from object be?
> I'm not really interested in _how_ to achieve such status (although of
> course it's very important, and should influence the decision); I'm
> interested in what's our contract with our users.

I think there are two answers: what an object MUST be and what it SHOULD be. 
Obviously, the object must be destructible. And at a minimum, the object 
should be assignable, so a new, valid state can be created.

This is the bare minimum. I'd also add that the moved-from object does not 
hold non-negligible resources still allocated. Classes that allocate O(n) or 
worse memory, not O(1) like the d pointer.

But I think all Qt classes should go beyond that, unless they have VERY good 
reasons not to do so (and document so). The moved-from object should also be 
in a valid state so all the accessor and mutation API in the class can operate 
in the object without ill effects. What they actually do, we can't tell, since 
the initial state is unknowable. So apply the principle of GIGO.

Usually, the easiest way to accomplish that is for the moved-from object to be 
reset to the default-constructed state. So this is what the Qt move 
constructors and move assignment operators should be aiming at, by default, 
unless they have reasons not to.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel System Software Products





More information about the Development mailing list