Here’s a snippet that compiles and works:
#include <iostream>
#include <string>
void print_str(const std::string& str) {
std::cout << str << 'n';
}
int main() {
std::string str = "Hello world";
print_str(std::move(str));
}
Godbolt
As far as I understand, std::move
has no effect here.
Two questions:
- Is it correct that
std::move
isn’t harmful here, and just has no effect? - Would it be considered good practice to use it anyway? If I don’t control
print_str
, and don’t needstr
after the call, one may say thatstd::move
will have a positive effect if at some pointprint_str
will be changed to getstd::string
by value.