-1

I would like to know how to write a move assignment operator in the following case:

class Foo : public vector<int>
{
public:
  Foo(Foo&& other) : vector<int>(move(other)) {};
  Foo& operator=(Foo&& other){ ????? };
};

How's it done?

Thanks, Tom

Tom Davies
  • 2,386
  • 3
  • 27
  • 44

1 Answers1

4
class Foo
    : public vector<int>
{
public:
    auto operator=( Foo&& other )
        -> Foo&
    {
        vector<int>::operator=( move( other ) );
        return *this;
    };

    Foo( Foo&& other )
        : vector<int>( move( other ) )
    {};
};

Or just

class Foo
    : public vector<int>
{
public:
    auto operator=( Foo&& other ) -> Foo& = default;

    Foo( Foo&& other )
        : vector<int>( move( other ) )
    {};
};

I'm not sure of the rules for automatic generation of move assignment operator, and would currently not yet rely on the compiler implementing those rules correctly (as of June 2014).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • why use the alternative function declaration syntax? does it matter? – risingDarkness Jun 13 '14 at 12:34
  • rather, since this is the only syntax that covers all cases, it's the main syntax. the old syntax is the alternate one, only supporting a subset of functions. why use an arbitrary mix of syntaxes instead of just using the main one everywhere. – Cheers and hth. - Alf Jun 13 '14 at 12:36