views:

332

answers:

3

I maybe asking a dumb question, but I looked at the wikipedia page for RVO here and could not stop wondering if that behavior is wrong. I tried it in my machine and RVO is fully kicked in despite optimization level. What if there was actually something BIG happenning in a constructor? I know it shouldn't, but what if? I can't understand why RVO would still happen when there are side effects in the constructor.

EDIT: -fno-elide-constructors seems to stop RVO. But the question remains.

EDIT2: On a more serious note, how many people know about something like this? It maybe in the standard, but it is still a really ugly feature as I see it. At least compilers should disable it by default and provide a switch for people who know about this. :)

EDIT 3: I still insist that this is really bad. :). I don't think I know of any other language constraint like this that goes directly against the syntax of language. Everything else throws either compiler or linker errors right?

+13  A: 

The standard mandates that operations with concern a program's observable state must not be optimized away, except for copy construction in certain circumstances. You must not rely on copy constructors to be executed, even if they have side effects you expect to see (e.g., console output).

sbi
So this is why passing by value is better?
nakiya
It's one reason why pass-by-value *might* be better. That's not universal advice either. Sometimes, pass-by-value enables optimizations that wouldn't be possible if passing by reference, but it may also be slower in some cases. :)
jalf
Further, I thought boost::shared_ptr depended on those same two functions for reference counting. What happens there?
nakiya
@nakiya: in the case of `shared_ptr`, creating an extra temporary object that gets destroyed almost immediately has the effect of incrementing then decrementing the reference count, so the reference count ends up the same whether the optimization happens or not.
Doug
@nakiya: well, if the copy constructor is optimized away, then it is because the copy is optimized away, and never created at all. And then it's ok that the reference counter isn't incremented (in fact, it's the only correct behavior). ;)
jalf
The compiler still isn't allowed to just perform a `memcpy` instead of calling the copy constructor. If it copies the object, it has to do so properly, using the copy constructor. But it's allowed to eliminate the copy entirely, and then it can also eliminate the copy constructor call.
jalf
umm, I don't get it. What is the temporary object you are talking about? Apologies for being slow.
nakiya
Oh, I get it :D
nakiya
I've never heard this to be true for copy-assignment. It only seems to allow to omit the copy for construction. Can you please point to the chapter/verse?
Johannes Schaub - litb
@Johannes: I can _never_ recite chapter and verse, I dread the legalize. I guess I remembered this one wrong, then. Changing my answer...
sbi
@Johannes: Better now?
sbi
@sbi I have no idea what's better. I'm not understanding the "rvo paragraph" good enough to judge :)
Johannes Schaub - litb
Seriously, how many C++ developers have read every corner of the standard? I for one certainly have not :(. Isn't it just plain dangerous to leave something like that as the default behavior? I mean, this could cause bugs in programs and people will just waste time. I'm not saying it's common, but I don't like it. Just what I think.
nakiya
@sbi: 12.8(15) is chapter and verse for return value optimization. Also, note that side effects in general can be optimized away if it doesn't affect the observable state of the program (the as-if rule), according to 1.9, s your first sentence still isn't quite right.
David Thornley
@nakiya: Return value optimization is found in lots of more advanced C++ books. I would venture to say that programs where copy constructors have side effects are likely to be buggy whether or not RVO is in effect, because I think it's a real bad idea.
David Thornley
@Johannes: Seeing that DerKuchen (how hilarious!) has already posted chapter and verse, and emphasized solely the elimination of a copy constructor, I removed the text mentioning assignment.
sbi
@David: Thanks for setting me straight on this. I'm changing this, too.
sbi
@nakiya: lots of things in C++ can cause bugs if the developer isn't aware of them. But if you write your copy constructor to be have "sensibly", that is, it constructs a copy, and nothing else, then it *just works* with and without RVO.
jalf
@sbi: if you read my comment to Klaim's answer, I may be the origin of the "copy assignment" error. If so, I apologize :-)
Matthieu M.
@sbi I wasn't so sure about the bullets. They allow a "copy" to be elided away. A copy assignment is a copy too. But as you say, the introduction text talks about "copy construction", so I feel that this is not about assignment too.
Johannes Schaub - litb
@Matthieu: I dunno, but IME I'm perfectly capable of messing things up on my own. `:)` @Johannes: Have I mentioned that I avoid reading the Holy Document like the plague? Can't stand their meter. `:(`
sbi
+4  A: 

Define "wrong". The C++ language explicitly permits this kind of optimization even though it is observable. If the behavior of your program depends on a specific implementation, then unfortunately you are not using ISO C++, but some dialect.

Helmut
+10  A: 

As said in the other answers, the compiler is allowed optimize away even non trivial copy constructors and assignment operators.

12.8.15

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted by constructing the automatic object directly into the function’s return value

— when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy

DerKuchen
+1 for the quote, please also provide a link to a copy of the standard for verification
Matt Joiner