abseil / Tip of the Week #11 : Return Policy

/11

  • Tip of the Week #11: Return Policy
    https://abseil.io/tips/11

    Originally posted as TotW #11 on August 16, 2012

    by Paul Chisholm

    Frodo: There’ll be none left for the return journey. Sam: I don’t think there will be a return journey, Mr. Frodo. – The Lord of the Rings: The Return of the King (novel by J.R.R. Tolkien, screenplay by Fran Walsh, Philippa Boyens, & Peter Jackson)

    Note: this tip, though still relevant, preceded the introduction of move semantics in C++11. Please read this tip with the advice from TotW #77 also in mind.

    Many older C++ codebases show patterns that are somewhat fearful of copying objects. Happily, we can “copy” without copying, thanks to something called “return value optimization” (RVO).

    RVO is a long-standing feature of almost all C++ compilers. Consider the following C++98 code, which has a copy constructor and an (...)