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. Other Discussions
  3. The Weird and The Wonderful
  4. What "for" ?

What "for" ?

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++question
19 Posts 13 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.
  • R Rage

    Just came across this (C++) #define for if( false );else for :omg:

    ~RaGE();

    I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
    Do not feed the troll ! - Common proverb

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #2

    :omg: One of the most horrific, IMHO.

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

    1 Reply Last reply
    0
    • R Rage

      Just came across this (C++) #define for if( false );else for :omg:

      ~RaGE();

      I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
      Do not feed the troll ! - Common proverb

      D Offline
      D Offline
      Doc Lobster
      wrote on last edited by
      #3

      This can be fixed easily: #define false true There ya go ...

      L 1 Reply Last reply
      0
      • R Rage

        Just came across this (C++) #define for if( false );else for :omg:

        ~RaGE();

        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
        Do not feed the troll ! - Common proverb

        M Offline
        M Offline
        MrMarco
        wrote on last edited by
        #4

        That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

        D Q R N A 6 Replies Last reply
        0
        • M MrMarco

          That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #5

          So the real WTF is that the author didn't write a paragraph long comment explaining the reason behind the definition?

          Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

          D P 2 Replies Last reply
          0
          • M MrMarco

            That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

            Q Offline
            Q Offline
            QuiJohn
            wrote on last edited by
            #6

            That's enlightening... It still makes it a horror, but it goes back to it being Microsoft's horror for the variable scope issue in VC6. Still, a comment by the #define in the original would have been helpful.


            Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

            L 1 Reply Last reply
            0
            • R Rage

              Just came across this (C++) #define for if( false );else for :omg:

              ~RaGE();

              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
              Do not feed the troll ! - Common proverb

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #7

              Similar than the macro style used for macro's that need to be an explicit statement. Eg:

              #define ASSERT(f) \
              do { \
              if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \
              FatalExit (0); \
              } while (0)

              This forces the 'user' to only use ASSERT(x) as a statement, as the ; is required. See link below for more info, or Google for "while(0)". http://www.thescripts.com/forum/thread215019.html[^]

              xacc.ide - now with IronScheme support
              IronScheme - 1.0 alpha 2 out now

              1 Reply Last reply
              0
              • Q QuiJohn

                That's enlightening... It still makes it a horror, but it goes back to it being Microsoft's horror for the variable scope issue in VC6. Still, a comment by the #define in the original would have been helpful.


                Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency!            -Emily Dickinson

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #8

                David Kentley wrote:

                but it goes back to it being Microsoft's horror for the variable scope issue in VC6

                It still cannot be used in C, C++ does allow it. GCC with GNU extensions does allow it too.

                xacc.ide - now with IronScheme support
                IronScheme - 1.0 alpha 2 out now

                1 Reply Last reply
                0
                • D Doc Lobster

                  This can be fixed easily: #define false true There ya go ...

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #9

                  or #define NULL !0 ;P

                  xacc.ide - now with IronScheme support
                  IronScheme - 1.0 alpha 2 out now

                  1 Reply Last reply
                  0
                  • D Dan Neely

                    So the real WTF is that the author didn't write a paragraph long comment explaining the reason behind the definition?

                    Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                    D Offline
                    D Offline
                    DevSolar
                    wrote on last edited by
                    #10

                    It didn't have to be a paragraph long, but at least one line would have been nice. Excellent exhibit for the next time someone tells you "real coders don't comment".

                    D 1 Reply Last reply
                    0
                    • D DevSolar

                      It didn't have to be a paragraph long, but at least one line would have been nice. Excellent exhibit for the next time someone tells you "real coders don't comment".

                      D Offline
                      D Offline
                      Dan Neely
                      wrote on last edited by
                      #11

                      everything is relative for most things a single line it adequate but for something that looks to be a major WTF though I prefer a generous explanation.

                      Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                      1 Reply Last reply
                      0
                      • M MrMarco

                        That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

                        R Offline
                        R Offline
                        Rage
                        wrote on last edited by
                        #12

                        MrMarco wrote:

                        one of my workmates:

                        :cool: Thanks to him for the explanation and to you for posting it here... I find it awesome that a true reason has been found to justify the mere existence of this line of code.

                        MrMarco wrote:

                        The macro you see is a workaround for this

                        :doh: So, we are speaking about using awful code to counter awful bugs. :rolleyes: Probably the climax of the coding horror (well, no, it could have been VB :) ).

                        ~RaGE();

                        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
                        Do not feed the troll ! - Common proverb

                        N 1 Reply Last reply
                        0
                        • M MrMarco

                          That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

                          N Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #13

                          MrMarco wrote:

                          That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6.

                          Exactly! However, in defense of good ol' VC6, it was released before the Standard, and in early 1990's some other compilers were doing the same thing.

                          Programming Blog utf8-cpp

                          1 Reply Last reply
                          0
                          • R Rage

                            MrMarco wrote:

                            one of my workmates:

                            :cool: Thanks to him for the explanation and to you for posting it here... I find it awesome that a true reason has been found to justify the mere existence of this line of code.

                            MrMarco wrote:

                            The macro you see is a workaround for this

                            :doh: So, we are speaking about using awful code to counter awful bugs. :rolleyes: Probably the climax of the coding horror (well, no, it could have been VB :) ).

                            ~RaGE();

                            I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
                            Do not feed the troll ! - Common proverb

                            N Offline
                            N Offline
                            Nemanja Trifunovic
                            wrote on last edited by
                            #14

                            Rage wrote:

                            So, we are speaking about using awful code to counter awful bugs.

                            That was really a feature, not a bug :) (see my other comment in this thread). It became a bug only after the Standard was released.

                            Programming Blog utf8-cpp

                            1 Reply Last reply
                            0
                            • R Rage

                              Just came across this (C++) #define for if( false );else for :omg:

                              ~RaGE();

                              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
                              Do not feed the troll ! - Common proverb

                              P Offline
                              P Offline
                              Paul Conrad
                              wrote on last edited by
                              #15

                              :wtf:

                              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                              1 Reply Last reply
                              0
                              • D Dan Neely

                                So the real WTF is that the author didn't write a paragraph long comment explaining the reason behind the definition?

                                Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                                P Offline
                                P Offline
                                Paul Conrad
                                wrote on last edited by
                                #16

                                dan neely wrote:

                                the real WTF is that the author didn't write a paragraph long comment explaining the reason behind the definition?

                                Exactly. I'd hate to be a newcomer on the project and run across that :laugh:

                                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                                1 Reply Last reply
                                0
                                • M MrMarco

                                  That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

                                  A Offline
                                  A Offline
                                  Aamir Butt
                                  wrote on last edited by
                                  #17

                                  interesting and enlightening... but then just for the sake of a little simplicity, it could have been written like #define for if(true)for; and yes, this thing should have been documented. I saw a similar thing somewhere else where there was code like..

                                  do
                                  {
                                  //code
                                  }
                                  while(0);

                                  This was just to define a scope for the variable declared with in the block. MS VC compilers do allow braces without any keyword behind them but in some flavors of C/C++, you just can't put braces in your code without a construct.

                                  My Articles

                                  L 1 Reply Last reply
                                  0
                                  • A Aamir Butt

                                    interesting and enlightening... but then just for the sake of a little simplicity, it could have been written like #define for if(true)for; and yes, this thing should have been documented. I saw a similar thing somewhere else where there was code like..

                                    do
                                    {
                                    //code
                                    }
                                    while(0);

                                    This was just to define a scope for the variable declared with in the block. MS VC compilers do allow braces without any keyword behind them but in some flavors of C/C++, you just can't put braces in your code without a construct.

                                    My Articles

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #18

                                    Thats a usefull code method to avoid using goto. A failure is followed by a break and you jump out of the loop. However, I use non keyworded braces my self for local vars, but also to bracket code that has a particular purpose, or I want to be thought of as distinct from the rest.

                                    Morality is indistinguishable from social proscription

                                    1 Reply Last reply
                                    0
                                    • M MrMarco

                                      That's the answer from one of my workmates: That's a workaround for one of the more braindead shortcomings of Microsoft Visual C++ 6. In ISO/ANSI C++, if you declare a loop variable inside a for-statement, that variable goes out of scope at the end of the loop, i.e. you can do this: for ( int i = 0; i < x; ++i ) { // Code } for ( int i = 0; i < y; ++i ) { // Code } MSVC 6 chokes on this - "variable redefinition". The macro you see is a workaround for this, "forcing" the loop variable into the else-scope (which MSVC 6 handles correctly).

                                      R Offline
                                      R Offline
                                      Rob Grainger
                                      wrote on last edited by
                                      #19

                                      A fair criticism, but remember that Visual C++ 6.0 was written before the standard was actually finalized, and at the time it was released that was one of the breaking changes.

                                      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