abseil / Tip of the Week #24 : Copies, Abbrv.

/24

  • Tip of the Week #24: Copies, Abbrv.
    https://abseil.io/tips/24

    Originally posted as TotW #24 on Nov 26, 2012

    by Titus Winters, (titus@google.com) and Chandler Carruth (chandlerc@google.com)

    “To copy others is necessary, but to copy oneself is pathetic.” - Pablo Picasso

    Note: see also TotW #55 and TotW #77 for guidance on name counting and copies vs. moves.

    One Name, No Copy; Two Names, Two Copies

    When evaluating whether copies get made within any given scope (including cases triggering RVO), check how many names your data refers to.

    You will have two copies of the data at any point where you have two live names for those copies. To a good first approximation, the compiler will (and often must) elide copies in all other cases.

    Between the move semantics of STL containers (introduced automatically with the switch to C++11) and copy constructor (...)