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).
The CCTBX tutorial documentation is great! However, I noticed an inaccuracy in the description of C++ and copy-optimization here:
cctbx.github.io/tour.html
Line 256 in d26097b
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
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).