0

Copy elision vs rvo

What is difference between copy elision and return value optimization?

30th Jan 2025, 5:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Respuestas
+ 2
Ketan Lalcheta Return Value Optimization is a subset of Copy Elision. Both are compiler optimizations designed to avoid resource/time costly copying of Class objects. Return Value Optimization focuses only on avoiding these copies in regard to functions which return-by-value. A distinction is made between 2 forms of RVO: 👉 named RVO - variable for Class object is created inside function and later passed as return value 👉 unnamed RVO - Class object created directly in the function's return statement Copy Elision more broadly covers: 👉 function return-by-value of Class objects 👉 initializing a Class object from another object of same Class 👉 passing Object values in try/throw - catch exception handling In the C++17 standard, certain categories of copy elision should be guaranteed to always occur, Especially unnamed return value optimization.
30th Jan 2025, 11:58 AM
Shardis Wolfe
+ 2
The question should be When is copy elision used in RVO. "Copy elision is a technique to skip calling the copy constructor so as not to pay for the overhead." That's the part that caught my attention from this SO thread: https://stackoverflow.com/questions/2143787/what-is-copy-elision-and-how-does-it-optimize-the-copy-and-swap-idiom
30th Jan 2025, 12:43 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li copy elision is always used in RVO. Or more correctly, RVO always uses copy elision. RVO is just the most common example of copy elision. That and the fact a distinction is made between named and unnamed RVO is why RVO has its own term.
30th Jan 2025, 4:23 PM
Shardis Wolfe
+ 1
Shardis Wolfe yes, but under what conditions the compiler enforce it have a lot of nuance. Feels like a good subject to explore.
30th Jan 2025, 5:14 PM
Bob_Li
Bob_Li - avatar