c++warningsdeprecation-warning

Is there a standard way to ignore deprecation attribute warnings


Since c++14 there is the standardized [[deprecated]] attribute to mark classes, functions, variables and so on as deprecated, which should emit a deprecation warning for each compiler.
Before c++14 this was only possible by compiler specific pragmas.

Now I want to use something deprecated and suppress the deprecation warning on this specific code location or in a certain area of code, e.g. due to a treat warnings as errors compiler setting.

I am aware that there are compiler specific ways like
GCCs:

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"  
#pragma GCC diagnostic warning "-Wdeprecated-declarations"

Visual Studio

__pragma(warning(push))  
__pragma(warning(disable:4996))  
__pragma(warning(pop))

Is there a standardized way to suppress deprecation warnings or do I have to stick with the compiler specific pragmas for every supported compiler?
I would love to get rid of such compiler specific macros.

Here an example code to work with for GCC:

#include <iostream>
    
#define BEGIN_IGNORE() _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
#define END_IGNORE() _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
    
[[deprecated]] void old_foo() { std::cout << "Better don't use this anymore\n"; }
    
int main()
{
    // I need to use it anyhow but don't want a compiler warning
    BEGIN_IGNORE();
    old_foo();
    END_IGNORE();
}

Solution

  • Is there a standardized way to suppress deprecation warnings

    Not that I'm aware of.

    or do I have to stick with the compiler specific pragmas for every supported compiler?

    Most likely, yes.


    On a side note: for GCC, instead of resetting the diagnostic level back to warning, you might consider using #pragma GCC diagnostic push and #pragma GCC diagnostic pop instead.


    An alternative solution to avoid the warning message is to simply move the deprecated code into a new function that is not marked as deprecated so you can call it where needed without warning, and then have the actual deprecated function call that new function so everywhere else still gets the warning, eg:

    #include <iostream>
    
    void old_foo_noWarning() { std::cout << "Better don't use this anymore\n"; }
    
    [[deprecated]] void old_foo() { old_foo_noWarning(); }
    
    int main()
    {
        // I need to use it anyhow but don't want a compiler warning
        //old_foo();
        old_foo_noWarning();
    }