How works deprecated warnings and how to remove them when using JsonCpp?

1.8k views Asked by At

I compiled with VS 2015 jsoncpp and am able to link with it and everythign works fine.

However, I'm getting tones of deprecated warnings. Some classes are marked as depecrated in the code:

class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {...};

with

#define JSONCPP_DEPRECATED(message) __declspec(deprecated(message))

Thing is I don't use those classes. I'm getting the messages as soon as the file is included. Compiling this:

#include <json/json.h>

int main( int argc, char* argv[] )
{

    return 0;
}

Produces 13 deprecated warnings...

Isn't those warnings only supposed to be reported when a deprecated class/function is used? is there a way to have it work this way? (I could disable warning C4996, but it would be better to keep it enabled, but only reported when a deprecated class/function is actually used).

2

There are 2 answers

2
FloIsAwsm On BEST ANSWER

I think the problem is, that some classes derive from Writer. This counts as being used. I have no idea how to get rid of the warnings, though.

EDIT: Tested it. It produces the same warning 5 times, without being used.

test.h

class __declspec(deprecated("Depricated Warning UnusedClass")) UnusedClass
{
public:
    void SetI(int &val);
};

class __declspec(deprecated("Depricated Warning UnusedClass")) UnusedClass2 : UnusedClass
{
public:
    int GetI();
    int i;
};

test.cpp

void UnusedClass::SetI(int &val)
{
    val = 0;
}

int UnusedClass2::GetI()
{
    return 10;
}

Warning:

Warning 7   warning C4996: 'UnusedClass': Depricated Warning UnusedClass    C:\Users\admin\Documents\Test.h 144
5
Motti On

As @FlosAwsm said, the problem is that the Writer class is derived from (even though the derived classes aren't used either).

I've submitted a pull request that fixes this issue, in the meantime you can perform the changes I made to your local copy of jsoncpp.

+++ include/json/writer.h
+#pragma warning(push)
+#pragma warning(disable:4996) // Deriving from deprecated class
class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer {
+#pragma warning(pop)

+#pragma warning(push)
+#pragma warning(disable:4996) // Deriving from deprecated class  
class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer {  
+#pragma warning(pop)  

Note that the warning was caused by FastWriter and StyledWriter deriving from deprecated class Writer. By disabling the warnings at the classes definitions we prevent the compiler from warning about this use, over which the code's client has no control.

Any other use (either directly of Writer or of either of the derived classes) will still produce a deprecation warning (which is the desired behaviour).