2

http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32

#include <iostream>
struct X {
    X(const char *) { std::cout << 1; }
    X(const X &) { std::cout << 2; }
    X(X &&) { std::cout << 3; }
};
X f(X a) {
    return a;
}
X g(const char * b) {
    X c(b);
    return c;
}

int main() {
    f("hello"); // 13
    g("hello"); // 1
}

Is there any difference in the last line of function f(X a): return a; instead of return std::move(a);?

Is it true that function f doesn't have RVO but g has NRVO?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
q0987
  • 34,938
  • 69
  • 242
  • 387

1 Answers1

3

Is there any difference in the last line of function f(X a): return a; instead of return std::move(a);?

No. a is a local variable of the function, so return a can move from it.

Is it true that function f doesn't have RVO but g has NRVO?

Correct. Named elision never applies to function parameters; it only applies to local variables that are not function parameters.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982