lundi 13 mars 2017

C++ RVO: when it happens?

http://ift.tt/2mF06j9

#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
}

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

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

Aucun commentaire:

Enregistrer un commentaire