Skip to content

C++ Programming Documentation and Copies #2

@frobnitzem

Description

@frobnitzem

The CCTBX tutorial documentation is great! However, I noticed an inaccuracy in the description of C++ and copy-optimization here:

<p>“Ouch” indicates that the <em>entire array</em> is copied when the function

Since C++11, return value optimization removes the implicit copy in return statements for most objects (STL containers in std). So, when constructing a vector via

std::vector<int> return_vector(void) {
  std::vector<int> tmp{1,2,3};
  return tmp;
}

std::vector<int> value = return_vector();

The destructor of tmp isn't called, and the second value is actually just directly pointing to tmp.ref This works for most (all?) containers in std. The key idea is std::move and reference values (rvalues), which are used in move constructors. This makes it a bit more complex to program in C++, but stick to the rule of 3/5 and you're usually good. Note that for pointers inside classes, this requires using smart pointers (Boost smart pointers have been adopted as std::shared_ptr).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions