6

In Visual C++ you can temporarily disable a warning by using pragma:

#pragma warning(suppress: 4307)

How can I disable a warning within a macro, e.g., when I cause an "integral constant overflow" warning like this:

#define TIMES_A_MILLION(x) x * 1000000
int value = TIMES_A_MILLION(4711);

I don't want to repeat the warning at every place where the macro is used, but want the suppression to be part of the macro it self.

It is obviously not possible to do like this:

#define TIMES_A_MILLION(x) \
#pragma warning(suppress: 4307) \
  x * 1000000
Jordfräs
  • 8,903
  • 3
  • 27
  • 30
  • 4
    Why would you want to suppress this warning instead of fixing the cause? – user7860670 Jan 18 '19 at 09:23
  • Possible duplicate of [What is \_\_pragma and what are the differences between \_\_pragma and #pragma](https://stackoverflow.com/questions/23790112/what-is-pragma-and-what-are-the-differences-between-pragma-and-pragma) – Max Vollmer Jan 18 '19 at 09:26
  • 1
    The question I voted yours to be duplicate of approaches this from exactly the other direction, but answers your question perfectly. There is also [this article](https://learn.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?view=vs-2017) by Microsoft. – Max Vollmer Jan 18 '19 at 09:26
  • "I don't want to repeat the warning at every place where the macro is used" - I would! – Bathsheba Jan 18 '19 at 09:42
  • The macro is just an example, in my case it is a hash calculation which uses a multiplication that intentionally overflows. – Jordfräs Jan 18 '19 at 09:47
  • @Jordfräs That begs two questions: 1. Why not use (possibly `constexpr` and/or templated) functions instead? 2. Storing something that overflows into an `int` is extremely suspicious because signed integer overflow is undefined behavior. – Max Langhof Jan 18 '19 at 10:20
  • Because I'm an old fart that haven't discovered constexpr, but I have already realized I probably should try that. My actual hash calculation is unsigned, if that makes more sense. – Jordfräs Jan 18 '19 at 13:53

1 Answers1

9

In your case you have to use the extension __pragma

__pragma

instead of

#pragma
darune
  • 10,480
  • 2
  • 24
  • 62