3 ms·
No, it is not necessarily identical in C, because even though you don't have constructors or destructors, you can make an address comparison to check whether tw
by BeeOnRope 8y ago
No, it is not necessarily identical in C, because even though you don't have constructors or destructors, you can make an address comparison to check whether two objects are identical.
For example the following code:
https://godbolt.org/g/ikD6bi https://godbolt.org/g/ikD6bi
where the address of the caller's variable that will take the return value is passed along to the function returning it. Within the callee, f1 and f2 must be different objects in C, but not in C++.
All decent compilers compile this in C++ with only a single object, so sink() will receive identical objects. Most compilers "deoptimize" this in C since sink cannot receive identical objects: two objects are created so f1 and f2 have different addresses, and a copy is performed from f2 to the return value f1.
Due to a bug, clang mis-compiles this and applies the C++ RVO to C. So you maybe you are right: you can get RVO in C, but only due to a compiler bug!
- stephencanon 8y agoAh, yes, that's a nice example.