+ 3
C++ questions in quiz section
There are lots of c++ questions of the type "guess the output" but are using two or more combination of postfix or prefix operators in an single expression. As per C++ standards, the order of evalution of operands in such expressions is undefined i.e. in int x=0; int z = ++x + x++; the value of z is undefined, because the order of evaluation is undefined, hence the result is undefined or compiler specific. Hence i think all such questions should be removed from c++ quiz section. What's your thought guys?
2 Answers
+ 9
I agree NeutronStar. I have made the same remark a while ago. There is nothing to learn in undefined behaviour quizzes - except to know that it is machine/compiler dependant.
+ 4
The format may not lend itself to highlighting its educational value:
* It affects all compilers (a useful detail)...because the operations occur simultaneously (at odds with each other) in the same "sequence point".
* Knowing that "sequence points" exist teaches how programming languages are made, how compilers are designed, optimize, etc.
* If this never came up and then one day it was in your codebase...would it be harder to debug?
* Knowing this is interesting+helpful in information security, should someone leverage a specific compiler's behavior.
* Another way to look at this is "unreliable". While you can't depend on the behavior across compilers, see the previous bullet.