Copy elision: tối ưu hóa của compiler — loại bỏ hoàn toàn các lần copy/move không cần thiết khi return object hoặc khởi tạo từ temporary.
RVO (Return Value Optimization): khi hàm trả về unnamed temporary, compiler xây dựng trực tiếp vào vị trí đích — không cần copy gì cả.
NRVO (Named RVO): tương tự nhưng với biến local có tên:
std::vector<int> makeData() {
std::vector<int> result; // NRVO: compiler xây trực tiếp vào
for (int i = 0; i < 1000; ++i) result.push_back(i);
return result; // KHÔNG copy 1000 phần tử
}
auto data = makeData(); // data được xây in-place từ đầuKể từ C++17: RVO với prvalue là bắt buộc (guaranteed copy elision) — không phụ thuộc compiler nữa. NRVO vẫn là optional optimization.
Ý nghĩa: viết code trả về object lớn bằng value thay vì qua pointer/out-param — vừa rõ ràng vừa không kém hiệu năng.
Copy elision: a compiler optimisation that completely eliminates unnecessary copies/moves when returning objects or initialising from temporaries.
RVO (Return Value Optimization): when a function returns an unnamed temporary, the compiler constructs it directly in the destination — zero copies.
NRVO (Named RVO): same but for named local variables:
std::vector<int> makeData() {
std::vector<int> result; // NRVO: compiler builds directly in place
for (int i = 0; i < 1000; ++i) result.push_back(i);
return result; // NO copy of 1000 elements
}
auto data = makeData(); // data is built in-place from the startSince C++17: RVO with prvalues is mandatory (guaranteed copy elision) — no longer compiler-dependent. NRVO remains an optional optimisation.
Takeaway: returning large objects by value instead of via pointer/out-param is both cleaner and not slower.