Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Disabling shortcut in Visual C++ [modified]

Disabling shortcut in Visual C++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jimfisher
    wrote on last edited by
    #1

    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

    I D 2 Replies Last reply
    0
    • J jimfisher

      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

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      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 and B 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...

      1 Reply Last reply
      0
      • J jimfisher

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • D David Crow

          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

          J Offline
          J Offline
          jimfisher
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups