4 ms·
Wow. The function produces identical code when inlined, so why does the C version require that memcpy when it's not inlined?
by syockit 5y ago
Wow. The function produces identical code when inlined, so why does the C version require that memcpy when it's not inlined?
- jcelerier 5y agobecause C++ allows RVO and not C (as this is technically a change in behaviour as one may instrument memcpy - there are also a few cases where the copying of the return value of a function is observable so this does not fall under the default "as-if" rule of optimization)
- flohofwoe 5y agoClang produces identical code between C++ and C: https://gcc.godbolt.org/z/vWqn4s5ev https://gcc.godbolt.org/z/vWqn4s5ev I don't think that C disallows RVO, gcc just might be more conservative there (maybe because of special Linux requirements)?
- jcelerier 5y ago> I don't think that C disallows RVO, optimizations should never change the output of a program unless explicitely allowed (the as-if rule). In c++ since RVO is allowed, it is ok for intptr_t value = 0; struct foo f() { struct foo the_foo; value = (intptr_t)&the_foo; return the_foo; } int main() { struct foo the_foo = f(); assert(value == (intptr_t)&the_foo); } to assert or not depending on the -O level. But I'd wager that it is not the case in C.