I have encountered code which does this:
SomeObject parse (std::istream && input) {....
The input
argument is an rvalue reference, which normally means the function is intended to take ownership of the argument. That's not quite what is happening here.
The parse
function will completely consume the input stream, and it demands an rvalue reference because then calling code will give away ownership of the istream
and hence this is a signal that the input stream will be unusable.
I think this is okay because, since the parse
function doesn't actually move the object around, there is no danger of slicing out the subtype. This is basically behaving as a normal reference from parse
's point of view, only there is a kind of compilable comment to the calling function that you have to give up ownership of the stream.
Is this code actually safe? Or is there some overlooked subtlety which makes this dangerous?