Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Truth! That dodges the warning nicely.

But check this out:

  #define SOME_MACRO( foo, bar )  \
      do                          \
      {                           \
          foo();                  \
          bar();                  \
      } while (0)  // warning: conditional expression
                   //   is constant
That's the only possible way to correctly enforce using a semicolon following a call to SOME_MACRO(a,b);

Most insidiously, you can't wrap the macro with #pragma warning (push, disable, pop) because it's a macro. So the warning can only be suppressed where you call it, not where it's defined!

That's why I globally disable this warning in all my projects. I've never been saved by it, and it's sucked away enough of my life.



Interesting, I just tried compiling this with gcc 4.4.5 and got no warnings:

  #include <stdio.h>
  #define ADD(x, y, z) \
  do { \
  z = x + y; \
  } while (0)

  int main(int argc, char *argv[])
  {
      int z;
      ADD(1, 3, z);
      printf("z = %d\n", z);
      return 0;
  }
If your compiler complains about the do{} while (0) construct in macros, I'd be inclined to treat it as a bug and complain to the vendor.


Good to know, thank you so much! Would you mind testing with -Wall etc? (And any other "give me more verbosity!" settings)

It was a poor design choice by Microsoft. All of their compilers emit the warning when using the highest warning level.

Thanks again --- if GCC never warns, then I feel much better about globally disabling it.


I don't get any warnings with -Wall and I get a "unused variables argc and argc" with -Wextra.

Changing main's signature to int main() eliminates all warnings at -Wextra.


Brilliant, thanks!

I've used up enough of your time already, but if you feel like it...:

Do you know if gcc has the equivalent of #pragma warning( disable: N ) ? i.e. could you disable your argc and argv warning for just that source file, and no other?

If yes, then is there a way to "scope" that disable to be within a header file? (#pragma warning(push) and #pragma warning(pop))


Yep, you can disable individual warnings with pragmas (http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html) so if I add

  #pragma GCC diagnostic ignored "-Wunused-parameter"
to the C file, I get warningless compilation even with -Wextra. you can also do push/pop to store/restore the warnings reporting options.


Thank you again!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: