vendredi 5 janvier 2018

Can I ensure RVO for reintrepret-cast'ed values?

Suppose I've written:

Foo get_a_foo() {
    return reinterpret_cast<Foo>(get_a_bar());
}

and suppose that sizeof(Foo) == sizeof(Bar).

Does return value optimization necessarily take place here, or are compilers allowed to do whatever they like when I "break the rules" by using a reinterpret_cast? If I don't get RVO, or am not guaranteed it - can I change this code to ensure that it occur?

My question is about C++11 and, separately, C++17 (since there was some change in it w.r.t. RVO, IIANM).

Aucun commentaire:

Enregistrer un commentaire