Is it possible to disable RVO (return value optimization) in Visual Studio 2010? Setting optimization flag to /Od
(turns off all optimizations) doesn't help. In g++ there exists flag -fno-elide-constructors
which disables RVO.
Asked
Active
Viewed 5,709 times
12
3 Answers
6
You cannot. It is just that simple. RVO/NRVO is Standard, and your code should not depend on it not being present.

Puppy
- 144,682
- 38
- 256
- 465
-
4As far as I can tell RVO/NRVO is **not** the Standard (see C++0x standard, section 12.8. Copying and moving class objects, paragraph 32). The Standard simply allows such optimization (g++ and VisualStudio implements it). I have no problem with that. But it would be nice to have some switch to disable it. For educational purposes maybe. Thank you, it's much clearer now :) – Goran Mar 30 '12 at 12:55
-
@Goran: If the Standard explicitly allows it, then it is Standard. – Puppy Mar 30 '12 at 13:33
-
4Allow != require. As such it isn't part of the standard, it merely won't contradict the standard. – Jörgen Sigvardsson Mar 30 '12 at 18:07
-
@Jorgen: If including it does not contradict the Standard, then it is Standard- i.e., you cannot ever write code that assumes that it is not included. – Puppy Mar 31 '12 at 12:01
-
1"If including it does not contradict the Standard, then it is Standard." I think it'd be more precise to say that "then it is allowed by the Standard.", and that does not necessarily mean that a compiler shouldn't provide a flag to turn it off. For instance I wanted to turn it off for educational purposes. – Mark Vincze Aug 14 '14 at 07:31
-
It's not standard. Based on the `standard draft(N296) 12.8 Copying and moving class object -- 31.3`, `the copy/move ctor op can be omitted by ...` which is what we call RVO here. I think it's saying compiler is allowed compiler to do such optimization. But it's not mandatory. Thus whether compiler do RVO or not follows the standard anyway. Do I misunderstand the standard? Thanks. – Qi W. Apr 03 '16 at 06:36
-
1@Puppy The issue here - i.e. what the question is possibly asking, and what I arrived at the question looking for - is that you *also* cannot ever write code that assumes that it **is** included, because it is Standard-compliant to not include it. Hence, a compiler which provides and respects an option to disable it is Standard-compliant with or without the option enabled. If I want to write portable code, I may want to test my code both with and without it enabled, to ensure that it will work with all Standard-compliant compilers. This is easiest with a compiler-provided flag to disable it. – Jamie S Jun 13 '20 at 19:34
-
1@JamieS That's the only presented motivation that actually makes some sense. I might comment that it's tricky to do this with testing because the exact situations where a compiler implements RVO and friends may differ between compilers or versions, so the preferable option is to simply not write objects that have impure copy/move constructors. That being said, you may have legacy code, etc, which does not do this correctly. – Puppy Jun 19 '20 at 12:48
2
Try to define your variable as volatile
, maybe solves your problem. If it does not, you should send come code...

Malkocoglu
- 2,522
- 2
- 26
- 32
-
+1; although this is just a random guess (and as such deserves downvotes) it is correct that `return my_volatile_variable;` does disable RVO. (C++11 §12.8/31 item 1.) – Potatoswatter Jan 05 '13 at 07:00
-
@Potatoswatter: I am curious why you thought of my answer as just a random guess. The "maybe" in my answer meant that I was not sure of this answer because I did not know what the real problem was. IMHO, this question is in the vein of http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Malkocoglu Jan 07 '13 at 10:42
-
Ah, sorry. I'd suppose that's the reason they downvoted you. (It's better to justify an answer involving `volatile`.) – Potatoswatter Jan 07 '13 at 13:08
-2
There is never any reason to disable this optimisation! What are you trying to achieve? It helps debug builds run faster without any bad side-effects at all. It also ensures code dependent on RVO or NRVO works identically in debug and release.

AshleysBrain
- 22,335
- 15
- 88
- 124
-
19I can think of one (at least to me) very important reason: education! How do you explain constructors, move/copy constructors, destructors in function calls to a student when compiler omits them!?! I'm very grateful for g++ for supporting such an option. – Goran Mar 30 '12 at 13:05
-
1I have to disable it to understand the lifetime of a returned value, I can't understand it now because it doesn't create the temporary, just copies it straight in. – Zebrafish Dec 21 '16 at 22:05
-
@Zebrafish If you're changing the lifetime of a returned value to understand the lifetime of a returned value, I don't think your understanding will be correct. – Puppy Jun 19 '20 at 12:45
-
@Goran You're teaching them wrong because their understanding will suddenly cease to apply when they try to use it in the real world... – Puppy Jun 19 '20 at 12:45