Is there any standard way of generating a compiler warning (or message), similar to the preprocessor directive #error
for generating errors? #warning
appears to exist in clang/gcc (and others), but is apparently not standard, and doesn't work in Visual Studio. #pragma message
is also supported in many compilers (newer gcc and MSVC), but is also non-standard.
Asked
Active
Viewed 66 times
0

MuertoExcobito
- 9,741
- 2
- 37
- 78
-
not a duplicate of http://stackoverflow.com/questions/11819240/how-to-write-compile-time-warning-for-user, which just asks how to write a warning for G++. I want a compiler independent warning. – MuertoExcobito Feb 22 '16 at 16:37
-
The answerer says MSVC will support #pragma message("your message here") – NickLamp Feb 22 '16 at 16:39
-
Right, but again, that's not standard either. I was more curious if there is some preprocessor foo that will work in all compilers to generate a warning. – MuertoExcobito Feb 22 '16 at 16:40
-
1How portable do you need it to be? There is no standard way so you need to define the amount of portability in order to get an actual solution. – NathanOliver Feb 22 '16 at 16:42
-
@NathanOliver well, as the question says, I'd like it to work in standard c++/preprocessor. Seems mildly frustrating that `#warning` it isn't part of the standard... don't think I've ever worked with a compiler that didn't have them. – MuertoExcobito Feb 22 '16 at 16:45
-
That is not enough. We need to know the versions. What needs to happen is you need to know what platforms you want to warning to work on. Then you need to write a macro that will call the correct way to do it depending on the compiler version. So if you want this to be completely portable then this will be a lot of work and would be too broad in my book. If you have a set of compilers/versions to work with then that narrowing should be enough to get this working. – NathanOliver Feb 22 '16 at 16:48
-
When distributing source code, you don't necessarily have control over which compilers are used. If the answer is simply 'no' there isn't, then that's fine - but, then this definitely isn't a duplicate of the other question. – MuertoExcobito Feb 22 '16 at 16:59
-
There is no standard way. You will have to use #ifdefs and write compiler specific implementations in a macro. If you can not modify the code there is no way. – drescherjm Feb 22 '16 at 17:04
-
Why not open the standard and search for the word "warning"? – n. m. could be an AI Feb 22 '16 at 18:30
-
@n.m - it doesn't say much about warnings in the standard. However, as I said, I'm wondering if there is a preprocessor construct, that will work to generate a warning on a c++ standard conforming compiler. Somewhat like there used by be for static_assert, before there was a standard static_assert (http://stackoverflow.com/questions/16437431/compile-time-assertion-in-c/16437558#16437558). – MuertoExcobito Feb 22 '16 at 18:55
-
How would anything generate a warning in a standard conforming compiler if the standard doesn't say what a warning is? You could just as well ask to generate a pizza. – n. m. could be an AI Feb 22 '16 at 19:08