mardi 21 juillet 2020

C++ std::move() performance wise with lvalues

I've just finished learning about lvalues, rvalues, move constructor and move operator. I can see the added value of using move constructor with rvalues performance and code wise. But i can't see the added value of using move operator with move constructor with lvalues performance wise, sure there is a added value code wise, but can't we achieve the same functionality for lvalues using some other technologies like pointers for example. so my question is: what is the added value of using move operator with move constructor with lvalues performance wise. Thanks for reading my question.

example:

class string{
public:
char* data;

string(){
    data = nullptr;
}

string(const char* p)
{
    size_t size = std::strlen(p) + 1;
    data = new char[size];
    std::memcpy(data, p, size);
}

 ~string()
{
    delete[] data;
}

string(const string& that)
{
    size_t size = std::strlen(that.data) + 1;
    data = new char[size];
    std::memcpy(data, that.data, size);
}

string(string&& that)
{
    data = that.data;
    that.data = nullptr;
}

string movee(string &that){
    data = that.data;
    that.data = nullptr;
}};

what is the difference performence wise:

string s1("test");
string s2(std::move(s1));

string s1("test");
string s2 = string();
s2.movee(s1);

Aucun commentaire:

Enregistrer un commentaire