jeudi 29 juillet 2021

When to use std::move() for std::string?

This is a bit loaded question since after reading about std::move(), I am more confused. std::move() will cast a variable to an rvalue.

My question.

  1. Does it make any sense to use std::move for primitive data type like std::string.

Now, if yes. Here is my code

std::string val;
Mypersist store();
store.restore(val);
setAttribute(val);
//after this I don't care about val. 


int Mypersist::store(std::string &value)
{

}

void setAttribute(const std::string &value)
{

}

I am already using reference rather than value copy so I am a bit confused if std::move makes sense here or not.

I have this in my mind.

Is using std::move(val) for setAttribute will help in performance? If so why?

std::string val;
Mypersist store();
store.restore(val);
setAttribute(std::move(val));
//after this I don't care about val. 

Aucun commentaire:

Enregistrer un commentaire