What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <string>
struct Box {
std::string v;
explicit Box(std::string s) : v(std::move(s)) {}
Box(const Box& o) : v(o.v) { std::cout << "copy "; }
Box(Box&& o) noexcept : v(std::move(o.v)) { std::cout << "move "; }
};
Box upper(Box b) {
for (char& c : b.v) c = static_cast<char>(c - 32);
return b; // a parameter: implicitly moved, never copied
}
int main() {
Box a("cpp");
Box r = upper(a);
std::cout << r.v << ' ' << a.v << '\n';
return 0;
}
a is copied into the parameter (it is an lvalue). Parameters cannot be elided on return, but they are treated as rvalues there, so the result is moved out.