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. Clever Code
  4. Thanks for the missing warning

Thanks for the missing warning

Scheduled Pinned Locked Moved Clever Code
game-devhelp
18 Posts 15 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.
  • M Mike Dimmick

    It's a perfectly valid expression-statement according to the C++ standard. Any expression can be used as a statement. This just happens not to do anything. The compiler is therefore not allowed to issue a diagnostic. The UNUSED_ALWAYS macro in (I think) ATL, used to indicate that a parameter is unused (which would cause a warning) simply evaluates to the contents of its parameter. That is, the definition is

    #define UNUSED_ALWAYS(x) (x)

    which therefore generates an expression-statement.


    DoEvents: Generating unexpected recursion since 1991

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

    Mike Dimmick wrote:

    It's a perfectly valid expression-statement according to the C++ standard. Any expression can be used as a statement. This just happens not to do anything. The compiler is therefore not allowed to issue a diagnostic.

    I have to disagree with this logic. It's not an error, but compiler warnings are all about letting you know about things that are legal but likely to be an error like: if (boolVal = false) Perfectly legal C, but 99% of the time what was intended was if (boolVal == false)

    -- Help Stamp Out and Abolish Redundancy The preceding is courtesy of the Department of Unnecessarily Redundant Repetition Department.

    A R 2 Replies Last reply
    0
    • D Dan Neely

      Mike Dimmick wrote:

      It's a perfectly valid expression-statement according to the C++ standard. Any expression can be used as a statement. This just happens not to do anything. The compiler is therefore not allowed to issue a diagnostic.

      I have to disagree with this logic. It's not an error, but compiler warnings are all about letting you know about things that are legal but likely to be an error like: if (boolVal = false) Perfectly legal C, but 99% of the time what was intended was if (boolVal == false)

      -- Help Stamp Out and Abolish Redundancy The preceding is courtesy of the Department of Unnecessarily Redundant Repetition Department.

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #9

      Ugh, the amount of times i've done that :sigh: If it where not for the compiler warning me, who knows how much time i could have wasted trying to figure out why stuff isn't working. Anyway, a 5 for an excellent point.

      My current favourite word is: PIE! I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for. -The Undefeated

      1 Reply Last reply
      0
      • M Mike Dimmick

        It's a perfectly valid expression-statement according to the C++ standard. Any expression can be used as a statement. This just happens not to do anything. The compiler is therefore not allowed to issue a diagnostic. The UNUSED_ALWAYS macro in (I think) ATL, used to indicate that a parameter is unused (which would cause a warning) simply evaluates to the contents of its parameter. That is, the definition is

        #define UNUSED_ALWAYS(x) (x)

        which therefore generates an expression-statement.


        DoEvents: Generating unexpected recursion since 1991

        M Offline
        M Offline
        Mauro Leggieri
        wrote on last edited by
        #10

        Yes, but it is a value :) Best regards, Mauro.

        1 Reply Last reply
        0
        • M Mauro Leggieri

          Hi, five minutes ago I was debugging a game I'm making and while tracing on this fragment of code: #define XBALLOON_TYPE_1 0x01 #define XBALLOON_TYPE_2 0x02 #define XBALLOON_TYPE_3 0x04 k = 0; if (nPosX < 332)   k |= XBALLOON_TYPE_1; if (nPosY < cBalloon.GetHeight()) {   nPosY += y2;   k |= XBALLOON_TYPE_2; } if (bBalloonIsThinking != FALSE)   XBALLOON_TYPE_3; ... I noticed that never could step in "if (bBalloonIsThinking != FALSE)". The error is that the code should be: if (bBalloonIsThinking != FALSE)   **k |=** XBALLOON_TYPE_3; But with the missing "k |=", the compiler (VS2005, in their default warning settings) says 0 errors, 0 warnings. Assume you are the compiler and found this: if (bBalloonIsThinking != FALSE)   0x04; At least, I suppose you will insult me. Best regards, Mauro.

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #11

          An expression statement isn't as rare as you might think. If you write k++;, that is an expression statement - it evaluates k++ (which happens to have a side-effect) and discards the result.

          --Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

          M 1 Reply Last reply
          0
          • M Michael Dunn

            An expression statement isn't as rare as you might think. If you write k++;, that is an expression statement - it evaluates k++ (which happens to have a side-effect) and discards the result.

            --Mike-- Visual C++ MVP :cool: LINKS~! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That's what's great about doing user interface work. No matter what you do, people will say that what you did was idiotic." -- Raymond Chen

            M Offline
            M Offline
            Mauro Leggieri
            wrote on last edited by
            #12

            Yes, but like I said before, the preprocesor replaces the macro with a value. If it were a variable the discussion would be different... but it is a simple number. C/C++ parser: If token = number and it is alone... just ignore it. Regards, Mauro.

            R 1 Reply Last reply
            0
            • M Mauro Leggieri

              Yes, but like I said before, the preprocesor replaces the macro with a value. If it were a variable the discussion would be different... but it is a simple number. C/C++ parser: If token = number and it is alone... just ignore it. Regards, Mauro.

              R Offline
              R Offline
              Robert Surtees
              wrote on last edited by
              #13

              unless the number is 42 which is known to have numerous side effects

              1 Reply Last reply
              0
              • D Dan Neely

                Mike Dimmick wrote:

                It's a perfectly valid expression-statement according to the C++ standard. Any expression can be used as a statement. This just happens not to do anything. The compiler is therefore not allowed to issue a diagnostic.

                I have to disagree with this logic. It's not an error, but compiler warnings are all about letting you know about things that are legal but likely to be an error like: if (boolVal = false) Perfectly legal C, but 99% of the time what was intended was if (boolVal == false)

                -- Help Stamp Out and Abolish Redundancy The preceding is courtesy of the Department of Unnecessarily Redundant Repetition Department.

                R Offline
                R Offline
                rlesavre
                wrote on last edited by
                #14

                That's why it's better to put constant first in your tests: if (false = boolVal) instead of: if (boolVal = false) :)

                B 1 Reply Last reply
                0
                • R rlesavre

                  That's why it's better to put constant first in your tests: if (false = boolVal) instead of: if (boolVal = false) :)

                  B Offline
                  B Offline
                  Big Daddy Farang
                  wrote on last edited by
                  #15

                  Excellent point. I've been doing that for years. Before I learned C, I was working in Pascal. In that language = is comparison and := is assignment. I got bit a few times. BDF

                  C 1 Reply Last reply
                  0
                  • B Big Daddy Farang

                    Excellent point. I've been doing that for years. Before I learned C, I was working in Pascal. In that language = is comparison and := is assignment. I got bit a few times. BDF

                    C Offline
                    C Offline
                    Craster
                    wrote on last edited by
                    #16

                    Big Daddy Farang wrote:

                    Before I learned C, I was working in Pascal. In that language = is comparison and := is assignment.

                    Yep. 'Equal to' and 'becomes equal to'. Perfectly logical. Why '=' and '==' were used in c syntax is one of the things that irritates me most about the language(s).

                    A 1 Reply Last reply
                    0
                    • C Craster

                      Big Daddy Farang wrote:

                      Before I learned C, I was working in Pascal. In that language = is comparison and := is assignment.

                      Yep. 'Equal to' and 'becomes equal to'. Perfectly logical. Why '=' and '==' were used in c syntax is one of the things that irritates me most about the language(s).

                      A Offline
                      A Offline
                      admiralh2
                      wrote on last edited by
                      #17

                      If I recall the justification from K&R, it was because assignment (=) was used far more often than equality (==), therefore assignment should be smaller. Remember, it *was* 1970.

                      Hopelessly pedantic since 1963.

                      N 1 Reply Last reply
                      0
                      • A admiralh2

                        If I recall the justification from K&R, it was because assignment (=) was used far more often than equality (==), therefore assignment should be smaller. Remember, it *was* 1970.

                        Hopelessly pedantic since 1963.

                        N Offline
                        N Offline
                        Nathan Tuggy
                        wrote on last edited by
                        #18

                        This makes sense, of course, but I have always thought that it would be really cool to have a "smart" = operator that, depending on context, is either comparison or assignment; a dedicated := assignment operator for when you're returning the result of an assignment; and a dedicated == operator for when you really want to compare. That way, you won't get bit, but you can have C's power if you need it. I'm not sure why no languages I've seen have this.

                        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