4

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).

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • You can still disable the specific warning with some `#pragma push` `#pragma pop` around the header. – Jarod42 Sep 11 '17 at 09:17
  • 1
    @Jarod42: Sure, but it would disable it even if I use a deprecated function...MSDN documentation says that `deprecated` reports C4996 only when the deprecated class is used....so why am I getting the warning by simply including the headre file? – jpo38 Sep 11 '17 at 09:30
  • BTW, I saw this behaviour in VS2012 but not in 2015. – Motti Sep 11 '17 at 13:34

2 Answers2

3

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
FloIsAwsm
  • 156
  • 1
  • 5
  • Should only be the case if the derived classes are actually used too...If class A derives from deprecated class B, as far as nobody instantiates A, there should be no sush warning... – jpo38 Sep 11 '17 at 10:42
  • I looked at the code and two classes derive from `Writer`, neither are used. – Motti Sep 11 '17 at 10:54
1

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).

Motti
  • 110,860
  • 49
  • 189
  • 262
  • 1
    Thanks. Note that you forgot a third class `StyledStreamWriter`. Also, `#pragma warning(pop)`has to be done at the end of class declaration..else, the warning remains (at least that's what I see with VS 2015) – jpo38 Sep 11 '17 at 14:48
  • 1
    Thanks, @jpo38. I've included your suggestion in Motti's pull-request. – cdunn2001 Sep 11 '17 at 18:47
  • @jpo38, I don't actually think that disabling the deprecation warnings on `StyledStreamWriter` is necessary. `StyledWriter` and `FastWriter` both derived from `Writer` which is deprecated (and thus are considered by VS to be using `Writer`). `StyledStreamWriter` is itself deprecated (same as `Writer`) and doesn't use a deprecated class. – Motti Sep 11 '17 at 19:52
  • @Motti: Thing is VS reports deprectaed warning for `StyledStreamWriter` attributes `childValues_`, `indentString_`, `indentation_` when json.h is included...it's unclear why it does that. – jpo38 Sep 12 '17 at 07:04
  • @jpo38 in that case I suggest following up with cdunn2001 on GitHub. – Motti Sep 12 '17 at 07:17