3

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.

Community
  • 1
  • 1
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
  • Could you disable the warning for the whole `MyClass.cpp` file? Just add `#pragma warning(disable: 4996)` right after all `#includes`? Don't even have to push/pop it. – Dialecticus Jul 27 '15 at 09:12
  • Yes but I'm wary about doing that in case I squash other warnings that I might actually care about. – Jon Cage Jul 27 '15 at 09:15
  • Hard to make sense of this, C++/CLI code should use the [Obsolete] attribute to generate deprecation warnings. But surely these old methods are at least calling themselves in a separate translation unit? Then simply use the preprocessor, wrap the declspecs with #ifndef SOMEMACRO... #endif. And define SOMEMACRO with the project or file settings. – Hans Passant Jul 27 '15 at 19:14
  • It was a slightly contrived example I put together (badly) from our codebase. You're right, that should have been a ref class. They're indeed called from a separate translation unit and @Dialecticus answer solves the issue neatly. I've made some edits to correct the shortcomings. – Jon Cage Jul 28 '15 at 11:23

1 Answers1

1

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
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • I think that would have the opposite effect i.e. I'd only see the deprecation warnings when the library is compiled.. ? – Jon Cage Jul 27 '15 at 09:55
  • Empty macro is defined in `MyClass.cpp` just before `#include`. This prevents the macro to be `#defined` to `__declspec(deprecated)` in `MyClass.h`. In all other source files `__declspec(deprecated)` is attached to the function. – Dialecticus Jul 27 '15 at 10:05
  • @JonCage (forgot to mention you in previous comment) – Dialecticus Jul 27 '15 at 10:35
  • Ah okay, that makes sense. That does seem neater than wrapping lots of code in pragma's :-) – Jon Cage Jul 27 '15 at 11:54