Pergunta

Is it legal/proper c++0x to leave an object moved for the purpose of move-construction in a state that can only be destroyed? For instance:

class move_constructible {...};

int main()
{
    move_constructible x;
    move_constructible y(std::move(x));
    // From now on, x can only be destroyed. Any other method will result
    // in a fatal error.
}

For the record, I'm trying to wrap in a c++ class a c struct with a pointer member which is always supposed to be pointing to some allocated memory area. All the c library API relies on this assumption. But this requirement prevents to write a truly cheap move constructor, since in order for x to remain a valid object after the move it will need its own allocated memory area. I've written the destructor in such a way that it will first check for NULL pointer before calling the corresponding cleanup function from the c API, so that at least the struct can be safely destroyed after the move.

Foi útil?

Solução

Yes, the language allows this. In fact it was one of the purposes of move semantics. It is however your responsibility to ensure that no other methods get called and/or provide proper diagnostics. Note, usually you can also use at least the assignment operator to "revive" your variable, such as in the classical example of swapping two values.

See also this question

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top