mardi 1 mars 2016

Is it legal to move the .str() member of a stringstream?

Consider the following example:

#include <sstream>
template <typename T>
inline std::string to_string(T const & op) {
    std::ostringstream result;
    result << op;
    return result.str();
}

If I was to return result, instead of result.str() it would automatically become an rvalue. Not so the string contained in in result (I assume). My expectation is that it is copied and the copy returned as an rvalue.

So my question here is, is it legal to:

return std::move(result.str());

I would assume it is, expecting the stream to be left with a valid, empty string. But I do not feel certain enough to actually do it.

Aucun commentaire:

Enregistrer un commentaire