Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent nested deprecated method calls from raising warnings?

I have some library code which has some legacy code I'd like to move away from. To do so, I've started marking the outdated methods as deprecated. Where those methods call each other, I get deprecation warnings which I'd rather not see (the new functionality means you just need a single call as less of the internals of the classes workings are exposed).

Is there a way to suppress the deprecation warning for the call from OldMethod to OldMethodHelper? ..or a better way to do this altogether?

For example (in MyClass.h):

public ref class MyClass
{
public:
    [Obsolete]
    void OldMethodHelper();

    [Obsolete]
    void OldMethod();

    void NewMethod();
};

In MyClass.cpp:

void MyClass::OldMethodHelper()
{
    // Some old helper method that's called both from within this class and externally.
}

void MyClass::OldMethod()
{
    OldMethodHelper(); // I don't want this call to raise a deprecation warning.
}

void MyClass::NewMethod()
{
    // A new method which replaces the calls to both of the previous methods.
}

Code is called like this:

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    MyClass m;
    m.OldMethodHelper(); // This should raise a deprecation warning.
    m.OldMethod(); // This should raise a deprecation warning.
    m.NewMethod();

    return 0;
}

Edit - I found another post on SO which suggests using #pragma warning(disable: 4996) is a possibility but it seems like a bit of a clunky way to approach the problem to me:

    void MyClass::OldMethod()
    {
#pragma warning(push)
#pragma warning(disable: 4996) //4996 for _CRT_SECURE_NO_WARNINGS equivalent
        OldMethodHelper(); // I don't want this call to raise a deprecation warning.
#pragma warning(pop)
    }

Edit2 - Made some corrections / clarifications to the code example.

like image 794
Jon Cage Avatar asked Dec 06 '25 07:12

Jon Cage


1 Answers

Speaking without proof, but maybe a macro could help here. Easier to show than to explain:

MyClass.h
---------

#ifndef MYCLASS_DEPRECATE
#define MYCLASS_DEPRECATE [Obsolete]
#endif

class MyClass
{
    MYCLASS_DEPRECATE void OldMethodHelper();

    ...
}

MyClass.cpp
-----------

#define MYCLASS_DEPRECATE
#include "MyClass.h"

// The rest of the code
like image 167
Dialecticus Avatar answered Dec 08 '25 22:12

Dialecticus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!