Today I saw the
Order of Evaluation section in C++ Primer, and I wished I could've learned this earlier!
This section stated that the order of evaluation in C++ is not defined (there are few exceptions), instead it's totally up to the compiler to decide which operand will be evaluated first.
int a = f1() * f2(); There are no guarantees that
f1() will be evaluated first or
f2() will be evaluated first.
Right right, now I recall a bug which took me some time to fix.
While I was working on the DOM Arena bug, I wrote something like this
HTMLElement* element = new(nodeInfo->NodeInfoManager()) HTMLElement(nodeInfo.forget());
A brief explanation to the code is
nodeInfo is a ref-counted smart pointer instance which owns a raw pointer, and
nodeInfo.forget() will make
nodeInfo to lose the ownership, to let it pass the ownership to the caller.
Do you see the problem? Yeah, if
nodeInfo.forget() gets evaluated first,
nodeInfo->NodeInfoManager() will became dereferencing a null pointer, which is invalid.
It was driving me nuts, because I couldn't understand why and I had the impression that the order of evaluation was from left to right!