Question

It seems there are a lot of questions about when a compiler will perform RVO or why it does not perform it in such-and-such a situation.

After reading http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ I can't seem to wrap my head around it either.

Basically it seems "usually" the compiler will perform some sort of optimization but there are rare cases when it can't.

If I understand the article by Dave Abrahams correctly he doesn't really suggest returning by value except for reasons of better readability. To me this suggests that for readability and consistency it might be a good idea to always pass in references for large output values.

So should I pass in references to avoid copying or is there some way to find out if the compiler will optimize away a copy?

Also, is the effort to find out if the compiler will optimize in a particular way actually less than just passing by reference?

Was it helpful?

Solution

The whole point of the article is to inform you that you should not worry about this. Optimisation is a black box that you should not care about except in the rarest of situations.

The compiler performs RVO when it deems RVO to be applicable.

OTHER TIPS

Any decent compiler will support it. Just go ahead and use it unless you know for sure that you need to support a compiler that doesn't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top