c++ - Is `x = std::move(x)` undefined? -


let x variable of type has been initialized. following line:

x = std::move(x) 

undefined? in standard , it?

no, not undefined behavior, going implementation defined behavior, depend on how move assignment implemented.

relevant lwg issue 2468: self-move-assignment of library types , note active issue , not have official proposal should considered indicative rather definitive, point out sections involved standard library , points out conflict. says:

suppose write

vector<string> v{"a", "b", "c", "d"}; v = move(v); 

what should state of v be? standard doesn't specific self-move-assignment. there's relevant text in several parts of standard, , it's not clear how reconcile them.

[...]

it's not clear text how put these pieces together, because it's not clear 1 takes precedence. maybe 17.6.4.9 [res.on.arguments] wins (it imposes implicit precondition isn't mentioned in moveassignable requirements, v = move(v) undefined), or maybe 23.2.1 [container.requirements.general] wins (it explicitly gives additional guarantees container::operator= beyond what's guaranteed library functions in general, v = move(v) no-op), or maybe else.

on existing implementations checked, it's worth, v = move(v) appeared clear vector; didn't leave vector unchanged , didn't cause crash.

and proposes:

informally: change moveassignable , container requirements tables (and other requirements tables mention move assignment, if any) make explicit x = move(x) defined behavior , leaves x in valid unspecified state. that's not standard says today, it's intended , it's consistent we've told users , implementations do.

note, built-in types copy, can see draft c++14 standard section 5.17 [expr.ass]:

in simple assignment (=), value of expression replaces of object referred left operand.

which different case classes, 5.17 says:

if left operand of class type, class shall complete. assignment objects of class defined copy/move assignment operator (12.8, 13.5.3).

note, clang has self move warning.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -