C++ 不推荐使用的警告如何工作,以及在使用JsonCpp时如何删除它们?

C++ 不推荐使用的警告如何工作,以及在使用JsonCpp时如何删除它们?,c++,jsoncpp,deprecation-warning,C++,Jsoncpp,Deprecation Warning,我使用VS 2015 jsoncpp进行编译,能够与之链接,并且每个符号都可以正常工作 然而,我收到了一些不赞成的警告。有些类在代码中标记为depacketed: class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer {...}; 与 问题是我不使用这些类。文件一包含进来我就会收到消息。汇编此文件: #include <json/json.h> int main( int argc, char*

我使用VS 2015 jsoncpp进行编译,能够与之链接,并且每个符号都可以正常工作

然而,我收到了一些不赞成的警告。有些类在代码中标记为depacketed:

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

问题是我不使用这些类。文件一包含进来我就会收到消息。汇编此文件:

#include <json/json.h>

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

    return 0;
}
#包括
int main(int argc,char*argv[])
{
返回0;
}
生成13个不推荐的警告


这些警告不应该只在使用不推荐使用的类/函数时报告吗?有没有办法让它这样工作?(我可以禁用警告C4996,但最好保持启用状态,但仅在实际使用不推荐的类/函数时报告)。

我认为问题在于,有些类是从Writer派生的。这被视为正在使用。不过,我不知道如何摆脱这些警告

编辑: 测试它。在未使用的情况下,它会产生5次相同的警告

测试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 7   warning C4996: 'UnusedClass': Depricated Warning UnusedClass    C:\Users\admin\Documents\Test.h 144
同样,问题在于
Writer
类是从派生的(即使派生类也没有使用)

我已经提交了一个解决此问题的解决方案,在此期间,您可以执行我对您的本地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)  
请注意,该警告是由
FastWriter
StyledWriter
派生自不推荐的类
Writer
引起的。通过禁用类定义处的警告,我们可以防止编译器对这种使用发出警告,因为代码的客户端无法控制这种使用


任何其他使用(直接使用
Writer
或任何派生类)仍将产生一个弃用警告(这是所需的行为)。

您仍然可以在标题周围使用一些
#pragma push
#pragma pop
来禁用特定警告。@Jarod42:当然,但是,即使我使用了不推荐的函数,它也会禁用它…MSDN文档中说,
不推荐的
仅在使用不推荐的类时报告C4996…那么,为什么我仅仅通过包含headre文件来得到警告呢?顺便说一句,我在VS2012中看到了这种行为,但在2015年没有看到。只有在实际使用派生类的情况下才会出现这种情况……如果类A派生自不推荐的类B,那么就没有人实例化A而言,应该没有sush警告……我查看了代码,两个类派生自
编写器
,都没有使用。谢谢。请注意,您忘记了第三个类
StyledStreamWriter
。另外,
#pragma warning(pop)
必须在类声明结束时完成。否则,警告仍然存在(至少这是我在VS 2015中看到的),谢谢@jpo38。我已经在莫蒂的请求中包含了你的建议。@jpo38,我并不认为有必要禁用
StyledStreamWriter
上的弃用警告
StyledWriter
FastWriter
都源于已弃用的
Writer
(因此被VS视为正在使用
Writer
StyledStreamWriter
本身已弃用(与
Writer
相同),并且未使用弃用的类。@Motti:VS报告了
StyledStreamWriter
属性
childValues
indentString
的弃用警告,
缩进
当包含json.h时……不清楚它为什么会这样做。@jpo38在这种情况下,我建议在GitHub上使用cdunn2001。
+++ 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)