vendredi 25 septembre 2015

rvalue reference and setup functions

I want to use convenience functions when creating class instances in my main() function to make things clearer.

Here is a minimal example:

class MyClass
{
    public:
            MyClass() : value{ -1 }, str{ "hello" } {}
            MyClass( const MyClass &&other )
            {
                    value = move( other.value );
                    str = move( other.str );
                    file = move( other.file ); //Use of deleted function...
            }

            void open()
            {
                    file.open( "myfile" );
            }

    private:
            MyClass( const MyClass & ) = delete;
            MyClass operator=( const MyClass & ) = delete;
            MyClass &operator=( const MyClass && ) = delete;

            ofstream file;
            int value;
            string str;
};

inline MyClass setup_myclass()
{
    MyClass ret;
    ret.open();

    return ret;
}

int main( int argc, char **argv )
{
    MyClass &&mc = setup_myclass();

    return 0;
}

Problem is when my class contains stuff like fstream or thread that has their move constructors deleted.

I am compiling with g++ 5.1.1 and arm-linux-g++ 5.2.0 (raspberrypi, buildroot).

What should my move constructor look like when I have members with deleted move constructors?

How can I change my code to have the same clean main function?

Aucun commentaire:

Enregistrer un commentaire