|
Boost : |
Subject: Re: [boost] [variant] Please vote for behavior
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-01-25 17:28:01
On Friday 25 January 2013 16:05:14 Gottlob Frege wrote:
> On Fri, Jan 25, 2013 at 3:16 PM, Krzysztof Czainski
<1czajnik_at_[hidden]>wrote:
> >
> > I think variant after move is like int without initialization:
> > int i;
> > cout << i; // don't do this
> > If the fact, that moved-from objects are only good for destruction or
> > assignment-to is accepted, then invariants for moved-from objects are
> > allowed to be violated.
>
> int i = 17;
> std::move(i);
> cout << i;
>
> don't do that because you don't know what value it will print, or because
> it will crash, cause "undefined behaviour", overwrite your harddrive?
>
> There's a difference between "don't know what value" and "UB".
I think you're mixing two different things here.
When it comes to uninitialized objects, there isn't any difference as far as
the standard is concerned. Whether you try to use an uninitialized (e.g. not
yet constructed) std::string or int it doesn't matter - either way it's
undefined behavior and anything can happen.
OTOH a moved-from object is not uninitialized, but its state is unspecified.
The only thing that can be said for certain about it is that it is capable of
being destroyed. I cannot find any other guarantees provided by the standard,
so I'd consider any other use of such objects as undefined, unless such
guarantees are explicitly provided by the interface documentation. Whether
this moved-from state violates or supplements object's invariants is a
rhethorical question, IMHO. The important part is that unless explicitly
documented otherwise, one should not expect any well-defined behavior
(including the ability of being reinitialized with assignment) from a moved-
from object.
As for the POD types, you can say they don't have a move constructor, so copy
is used instead. This makes the use of moved-from PODs defined, but that's a
special case.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk