Disabling shortcut in Visual C++ [modified]
-
In C code an if statement such as: if(A || B) will shortcut, i.e. if A is true, it will branch and not even check B. On the other hand in the case of: if(A && B) a shortcut branch is taken if A is false and B is not even checked. While this is often useful, I have a situation where I would like to disable shortcuts. Is this possible in VC++? Jim Fisher -- modified at 0:35 Tuesday 14th August, 2007
-
In C code an if statement such as: if(A || B) will shortcut, i.e. if A is true, it will branch and not even check B. On the other hand in the case of: if(A && B) a shortcut branch is taken if A is false and B is not even checked. While this is often useful, I have a situation where I would like to disable shortcuts. Is this possible in VC++? Jim Fisher -- modified at 0:35 Tuesday 14th August, 2007
Its pretty deep in the assumptions of C that you will have this shortcutting behaviour, and as you mention, is very useful. ie.
if (pointer && pointer->otherpointer)
{
...
}would crash if pointer was null, and there wasn't this shortcutting behaviour. I'm assuming that
A
andB
are complex expressions, or function calls. In which case you can do...BOOL A = Something_I_Insist_On_Being_Done ();
BOOL B = Other_Thing ();if (A || B)
{
...
}If a compiler flag did exist then you'd have terrible trouble understanding why you used it when you look back in two years time. To avoid that, you'd end up writing so many comments you may as well have done the preconditions early as I did in my example. Iain. ps. Never forget the impact on your successor - especially as that may be you on a monday morning with a hangover...
-
In C code an if statement such as: if(A || B) will shortcut, i.e. if A is true, it will branch and not even check B. On the other hand in the case of: if(A && B) a shortcut branch is taken if A is false and B is not even checked. While this is often useful, I have a situation where I would like to disable shortcuts. Is this possible in VC++? Jim Fisher -- modified at 0:35 Tuesday 14th August, 2007
It's called short-circuit evaluation, and has been part of C for decades. Why would you want to "disable" it?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
It's called short-circuit evaluation, and has been part of C for decades. Why would you want to "disable" it?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
when building software for avionics (DO-178B) Level A (critical to safety) one must test every path in the code. The difficult paths to prove they have been taken are generally the "shortcut" paths. Thus it is often easier to test the crash cases yourself as: if(pointer != NULL) { if(pointer->object) { do whatever One can still do this even with short cutting, it is just expressions such as: if((A || B) && (C || D)) These have several shortcut paths. To defeat this, one could convert the bools to unsigned int and // no shortcutting on bit ops as the compiler thinks I need all 16 bits X = (A | B) & (C | D); if(X != 0) { do this or that But this seems more awkward and harder to manage (code reviews etc) than just defeating the shortcut code generation... Make sense or no? Jim Fisher