Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does copy elision exist in C?

As I read about copy elision, many of the sources only mention C++ and not C.

They state how the C++ standard allows this optimization to take place if the compiler decides it is the right thing to do.

But what about C? Do C compilers perform copy elision, or does the C standard gaurentee that copies will never be optimized away?

like image 838
Trevor Hickey Avatar asked Jul 03 '15 06:07

Trevor Hickey


1 Answers

Both C and C++ allow any optimizations that follow the as-if rule. Since C doesn't have constructors - and therefore constructors with side-effects- copies may be elided without breaking this rule. C++ on the other hand needs to make a special case where an implementation is allowed to break as-if.

So, copy elision exists in C implicitly through the as-if rule.

like image 196
juanchopanza Avatar answered Oct 19 '22 14:10

juanchopanza