Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are std::streams already movable?

GNU gcc 4.3 partially supports the upcoming c++0x standard: among the implemented features the rvalue reference. By means of the rvalue reference it should be possible to move a non-copyable object or return it from a function.

Are std::streams already movable by means of rvalue reference or does the current library implementation lack something?

like image 329
Nicola Bonelli Avatar asked Jul 12 '26 14:07

Nicola Bonelli


2 Answers

In the current g++ svn, rvalue reference support has not yet been added to streams. I suspect adding it will not be too difficult and as ever with open source software, patches are, I'm sure, welcome!

like image 138
Chris Jefferson Avatar answered Jul 14 '26 02:07

Chris Jefferson


After a quick investigation it comes out that the rvalue reference support has not been added yet to streams.

To return a non-copyable object from a function indeed it is sufficient to implement the move constructor as follows:

struct noncopyable
{
    noncopyable()
    {}

    // move constructor
    noncopyable(noncopyable &&)
    {}

private:
    noncopyable(const noncopyable &);
    noncopyable &operator=(const noncopyable &);
};

Such constructor is supposed to transfer the ownership to the new object leaving the one being passed in a default state.

That said, it is possible to return an object from a function in this way:

noncopyable factory()
{
    noncopyable abc;
    return std::move(abc);
}

While std::stream does not support move constructors it seems that STL containers shipped with gcc 4.3.2 do already support it.

like image 24
Nicola Bonelli Avatar answered Jul 14 '26 03:07

Nicola Bonelli