Trevor Hickey
Trevor Hickey

Reputation: 37914

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?

Upvotes: 5

Views: 399

Answers (1)

juanchopanza
juanchopanza

Reputation: 227538

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.

Upvotes: 6

Related Questions