Skip to main content
added 279 characters in body
Source Link
Philipp Claßen
  • 44.5k
  • 36
  • 163
  • 256

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When should you avoid std::move, even if it would be safe? I wouldn't use it in trivial examples, e.g.,:

 int x = 0; int y = std::move(x); 

Beside that, I see no downsides. If it does not complicate the code, moving should be done whenever possible IMHO.

Another example, where you don't want to move are return values. The language guarantees that return values are (at least) moved, so you should not write

return std::move(x); // not recommended 

(If you are lucky, return value optimization hits, which is even better than a move operation.)

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When should you avoid std::move, even if it would be safe? I wouldn't use it in trivial examples, e.g.,:

 int x = 0; int y = std::move(x); 

Beside that, I see no downsides. If it does not complicate the code, moving should be done whenever possible IMHO.

Another example, where you don't want to move are return values. The language guarantees that return values are (at least) moved, so you should not write

return std::move(x); // not recommended 

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When should you avoid std::move, even if it would be safe? I wouldn't use it in trivial examples, e.g.,:

 int x = 0; int y = std::move(x); 

Beside that, I see no downsides. If it does not complicate the code, moving should be done whenever possible IMHO.

Another example, where you don't want to move are return values. The language guarantees that return values are (at least) moved, so you should not write

return std::move(x); // not recommended 

(If you are lucky, return value optimization hits, which is even better than a move operation.)

added 279 characters in body
Source Link
Philipp Claßen
  • 44.5k
  • 36
  • 163
  • 256

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When should you avoid std::move, even if it would be safe? I wouldn't use it in trivial examples, e.g.,:

 int x = 0; int y = std::move(x); 

Beside that, I see no downsides. If it does not complicate the code, moving should be done whenever possible IMHO.

Another example, where you don't want to move are return values. The language guarantees that return values are (at least) moved, so you should not write

return std::move(x); // not recommended 

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.

When should you avoid std::move, even if it would be safe? I wouldn't use it in trivial examples, e.g.,:

 int x = 0; int y = std::move(x); 

Beside that, I see no downsides. If it does not complicate the code, moving should be done whenever possible IMHO.

Another example, where you don't want to move are return values. The language guarantees that return values are (at least) moved, so you should not write

return std::move(x); // not recommended 
Source Link
Philipp Claßen
  • 44.5k
  • 36
  • 163
  • 256

When you have an object of type T&&, a rvalue, it means that this object is safe to be moved, as no one else will depend on its internal state later.

As moving should never be more expensive than copying, you will almost always want to move it. And to move it, you have to use the std::move function.