
15 Apr
2007
15 Apr
'07
12:04 a.m.
Joe Gottman wrote:
shared_ptr<int> p(new int(1)); foo(move(p)); if (p) { // foo didn't want it, try the next potential consumer bar(move(p)); } else { return; // foo consumed our shared_ptr, so we are done. }
An interesting pattern, thanks for the example. A bit of a circular logic here, since for this example to work reliably shared_ptr must clear the source of the move; therefore, if we don't specify it to clear the source, this example will not be valid. (It isn't in general; a raw pointer, for example, is not zeroed when moved from.) But it may well be a pattern that we decide to support. This implies an "overspecified" assignment, though; the straightforward swap doesn't clear the source.