-
Notifications
You must be signed in to change notification settings - Fork 1
Description
The CCTBX tutorial documentation is great! However, I noticed an inaccuracy in the description of C++ and copy-optimization here:
Line 256 in d26097b
| <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).