CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    [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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    I looking for a standard approach rather than platform specific.

    Thanks.
    Thanks for your help.

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: Defining a macro to disable warnings with pragma

    it's ide specific, not platform specific, right?

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by Amleto View Post
    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.

  7. #7
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  8. #8
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by _Superman_ View Post
    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
    Quote Originally Posted by Kheun View Post
    _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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  10. #10
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Defining a macro to disable warnings with pragma

    Quote Originally Posted by Kheun View Post
    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.

  11. #11
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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)
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  12. #12
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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 )
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  13. #13
    Join Date
    Apr 2008
    Posts
    725

    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.

  14. #14
    Join Date
    Apr 2008
    Posts
    725

    Re: Defining a macro to disable warnings with pragma


  15. #15
    Join Date
    May 2007
    Posts
    811

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured