News for hackers. Stuff that matters.
Re:Modern C++ for C Programmers: part 5 by stephencanon on Wednesday July 25, 2018 @12:13PM (#17610370)
It's possible that I'm missing something, but I believe that the result is necessarily identical in C, because there can be no observable side-effect of a struct copy in the C abstract machine.
Re:Modern C++ for C Programmers: part 5 by BeeOnRope on Wednesday July 25, 2018 @05:49PM (#17613197)
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

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!

Re:Modern C++ for C Programmers: part 5 by stephencanon on Wednesday July 25, 2018 @06:05PM (#17613351)
Ah, yes, that's a nice example.
Re:Modern C++ for C Programmers: part 5 by 8xde0wcNwpslOw on Wednesday July 25, 2018 @02:54PM (#17611804)
Don't quote me on this, but I think padding bytes might have different values in certain cases.
Re:Modern C++ for C Programmers: part 5 by a1369209993 on Wednesday July 25, 2018 @03:01PM (#17611857)
I'm fairly sure it's always undefined behavior to observe those differences, so while it's possible in practice, it'd never work in theory.
Re:Modern C++ for C Programmers: part 5 by e12e on Wednesday July 25, 2018 @05:49PM (#17613198)
> while it's possible in practice, it'd never work in theory.

Hah, sounds like you nailed the emergent philosophy of C there... :-)

Re:Modern C++ for C Programmers: part 5 by SidiousL on Wednesday July 25, 2018 @12:38PM (#17610583)
I tried to find an example that contradicts what you said, but I can't find any. Here's an online example which can be compiled as both C and C++ and I obtain exactly the same assembly using both clang and gcc. Maybe somebody can tweak this example to force the C compiler not to perform the optimization.

https://godbolt.org/g/7Ewun7

Re:Modern C++ for C Programmers: part 5 by BeeOnRope on Wednesday July 25, 2018 @05:51PM (#17613217)
See my reply to the parent poster for an example where RVO applies in C++ but not C, leading to better code in the C++ case. The key is to let the address of the objects involved escape: otherwise "as if" will let C (and C++ even without RVO) optimize away the copies anyways, since they are not observable in that case.