Problems compiling C ++ assignment operators

The following code does not work with gcc 4.8.0 (mingw-w64) with -O2 -std = C ++ 11 -frtti -fexceptions -mthreads

#include <string>

class Param
{
public:
    Param() : data(new std::string) { }

    Param(const std::string & other) : data(new std::string(other)) { }

    Param(const Param & other) : data(new std::string(*other.data)) { }

    Param & operator=(const Param & other) {
        *data = *other.data; return *this;
    }

    ~Param() {
        delete data;
    }

    Param & operator=(Param &&) = delete;


private:
    std::string * data;
};


int main()
{
    Param param;
    param = Param("hop");


    return 0;
}

With an error: error: using the remote function "Param & Param :: Operator = (Param & &)" On the line:

param = Param ("hop");

And it compiles well if I delete the line to remove the transfer destination.

There should not be a default assignment operator by default, since there are user-defined copy constructors, a user-defined copy assignment, and destructors, so deleting this file should not affect compilation, why doesn't it work? And why does distribution simply not use copy assignment?

+4
1

, main. , , . , rvalue (Param("hop")), , . , , , . , .

, :

class X
{
  void f(int) {}
  void f(short) = delete;
};

int main()
{
  X x;
  short s;
  x.f(s);  // error: f(short) is deleted.
}

f(short) , f(int) , , .

+7

Source: https://habr.com/ru/post/1536010/


All Articles