-
May 29th, 2009, 10:01 PM
#1
[RESOLVED] Defining a macro to disable warnings with pragma
Hey.
I want to do this:
Code:
#ifndef DISABLE_WARNING_H
#define DISABLE_WARNING_H
#define PUSH_DISABLE_WARNING(warning)
#pragma warning(push)
#pragma warning(disable : 4800)
#define POP_DISABLE_WARNING()
#pragma warning(pop)
#endif
But it's clearly not working... it was a quick attempt since I know that I don't have a clue on how to code it...
Any help?
Cheers.
-
May 30th, 2009, 01:47 AM
#2
Re: Defining a macro to disable warnings with pragma
For #define, a new line is the terminating character.
So put a backslash to indicate that the line continues to the next.
Code:
#ifndef DISABLE_WARNING_H
#define DISABLE_WARNING_H
#define PUSH_DISABLE_WARNING(warning) \
#pragma warning(push) \
#pragma warning(disable : 4800)
#define POP_DISABLE_WARNING() \
#pragma warning(pop)
#endif
-
May 30th, 2009, 02:11 AM
#3
Re: Defining a macro to disable warnings with pragma
_Superman_, the problem is not about writing a macro but on using preprocessor to suppress warning messages from the compiler on certain part of the code. One example would be reusing a legacy code that causing the compiler to generate warning message. As just any good programmer, we must not ignore the compiler warning. However, since you may not have the right to change the legacy code, you only want to suppress the compiler warning from the legacy code but from other part of the code.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 30th, 2009, 03:38 AM
#4
Re: Defining a macro to disable warnings with pragma
I looking for a standard approach rather than platform specific.
Thanks.
Thanks for your help.
-
May 30th, 2009, 06:33 PM
#5
Re: Defining a macro to disable warnings with pragma
it's ide specific, not platform specific, right?
-
May 31st, 2009, 03:04 AM
#6
Re: Defining a macro to disable warnings with pragma
Originally Posted by Amleto
it's ide specific, not platform specific, right?
I looking for standard approach rather than compiler/IDE specific.
Does boost offer this kind of feature ?
Thanks.
Thanks for your help.
-
May 31st, 2009, 03:40 AM
#7
Re: Defining a macro to disable warnings with pragma
I doubt so. As far as I know, #pragma preprocessor directive is compiler specific. I don't think it is part of the C++ language.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 31st, 2009, 03:54 AM
#8
Re: Defining a macro to disable warnings with pragma
Originally Posted by _Superman_
For #define, a new line is the terminating character.
So put a backslash to indicate that the line continues to the next.
Code:
#ifndef DISABLE_WARNING_H
#define DISABLE_WARNING_H
#define PUSH_DISABLE_WARNING(warning) \
#pragma warning(push) \
#pragma warning(disable : 4800)
#define POP_DISABLE_WARNING() \
#pragma warning(pop)
#endif
Hey. I tried that and get errors:
Code:
#define PUSH_DISABLE_WARNING(warning_) \
#pragma warning(push) \
#pragma warning(disable : warning_)
#define POP_DISABLE_WARNING() \
#pragma warning(pop)
int main(int argc, char* argv[])
{
return 0;
}
Code:
Error 1 error C2162: expected macro formal parameter c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp 2
Error 2 error C2162: expected macro formal parameter c:\documents and settings\bill\my documents\visual studio 2005\projects\test\test\main.cpp 3
Originally Posted by Kheun
_Superman_, the problem is not about writing a macro but on using preprocessor to suppress warning messages from the compiler on certain part of the code. One example would be reusing a legacy code that causing the compiler to generate warning message. As just any good programmer, we must not ignore the compiler warning. However, since you may not have the right to change the legacy code, you only want to suppress the compiler warning from the legacy code but from other part of the code.
Not quite sure what you meant to say here, as the problem is indeed how to write a macro that suppresses warning messages.
Cheers.
-
May 31st, 2009, 04:02 AM
#9
Re: Defining a macro to disable warnings with pragma
Thanks for clearing up.
I found this link that may help but I haven't tried it.
http://msdn.microsoft.com/en-us/library/d9x1s805.aspx
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
June 2nd, 2009, 04:00 AM
#10
Re: Defining a macro to disable warnings with pragma
Originally Posted by Kheun
I doubt so. As far as I know, #pragma preprocessor directive is compiler specific. I don't think it is part of the C++ language.
Although there aren't any language feature support it, but i think [preprocessor if] should be able to cope with it.
Thanks for your help.
-
May 31st, 2009, 12:48 AM
#11
Re: Defining a macro to disable warnings with pragma
I have done this many times and it did work.
This is what I do.
#pragma warning(disable:4800)
Some code or #include which throws the warning.
#pragma warning(default:4800)
-
May 31st, 2009, 04:22 AM
#12
Re: Defining a macro to disable warnings with pragma
Thanks for the link. I appear to be using it correctly, so I think my macro is coded wrong. From the MSDN link:
Code:
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#pragma warning( pop )
-
May 31st, 2009, 04:40 AM
#13
Re: Defining a macro to disable warnings with pragma
i think its expecting the thing after # in #pragma to be a macro argument. I dont know much about macros, but # and ## are used for token pasting and things like that.
try this:
Code:
#define myPragma (#pragma)
#define PUSH_DISABLE_WARNING(warning_) \
myPragma warning(push)\
myPragma warning(disable : warning_)
#define POP_DISABLE_WARNING() \
#pragma warning(pop)
int main(int argc, char* argv[])
{
return 0;
}
don't know why it's not needed for the warning (pop) part though. I believe it is because no argument is given to the macro..
Last edited by Amleto; May 31st, 2009 at 04:46 AM.
-
June 3rd, 2009, 05:32 PM
#14
Re: Defining a macro to disable warnings with pragma
-
June 3rd, 2009, 07:26 PM
#15
Re: Defining a macro to disable warnings with pragma
I would ask what are you gaining here by using macro. You just obscure it and maybe save one line of typing code.
If you forget the syntax for it which happens also to me , I just have pasted in one of my header files in a comment block, and then just copy and paste when I needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|