Consider the following example:
#include <optional>
#include <string>
struct S {
std::string text = "hello";
};
std::optional<S> foo() {
S s;
return s;
}
std::optional<S> bar() {
S s;
return std::move(s);
}
Am I correct that foo
and bar
are identical since the compiler does RVO and automatically moves s
when passing it to std::optional<S>
?
So the general rule “Don’t move what you return” applies also when an implicit conversion will happen?