是否有跨平台的方法可以在C++中禁用已弃用的警告?
Is there a cross-platform way to disable deprecated warnings in C++?
我有一个库,我正在重构一些功能。我用下面定义的itkLegacyMacro
标记了一些旧方法。这些已弃用的方法将从库自己的单元测试中调用。有没有办法禁用适用于所有(或至少大多数(编译器的弃用警告?
itkLegacyMacro:
// Setup compile-time warnings for uses of deprecated methods if
// possible on this compiler.
#if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
#define itkLegacyMacro(method) method __attribute__( ( deprecated ) )
#elif defined( _MSC_VER )
#define itkLegacyMacro(method) __declspec(deprecated) method
#else
#define itkLegacyMacro(method) method
#endif
库中的方法定义:
class X {
itkLegacyMacro(void oldMethod());
void newMethod(); }
从单元测试调用的方法:
X testX;
testX.newMethod(); //test the new stuff
testX.oldMethod(); //test the old stuff too!
最后一行会导致在编译时发出警告。我希望这个库测试已弃用的功能,但在编译时没有警告。这可能吗?正在使用C++11。
据我所知__declspec(...)
是一个Microsoft扩展,无论如何都不是跨平台的。
您可以使用宏来控制它
#ifdef _MSC_VER
#define DEPRECATED __declspec(deprecated)
#else
#define DEPRECATED
#endif
DEPRECATED void someDeprecatedFunction()
自C++14以来也有[[deprecated("because")]]
。
若要仅为单元测试关闭它,可以执行以下操作
#ifndef SUPPRESS_DEPRECATE_FUNCTIONS
#define DEPRECATED __declspec(deprecated)
#else
#define DEPRECATED
#endif
然后在单元测试中#define SUPPRESS_DEPRECATE_FUNCTIONS
,或使用-DSUPPRESS_DEPRECATE_FUNCTIONS
编译。或者,您可以在单元测试中创建一个特殊的标头,以#pragma
警告抑制。类似的东西
#if defined( __GNUC__ ) && !defined( __INTEL_COMPILER ) && ( __GNUC__ > 3 || ( __GNUC__ == 3 && __GNUC_MINOR__ >= 1 ) )
#pragma for gcc
#elif defined( _MSC_VER )
#pragma for msvc
#else
// nothing
#endif
#include "your_library_header.h"
然后,单元测试仅在任何其他库标头之前包含此标头。
相关文章:
- 警告处理为错误这里有什么问题
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 通过网络、跨平台传递std::变体是否安全
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 使用typeid警告未使用的变量
- 示例C++项目编译中的警告
- 如何跨平台将二进制资源构建到程序中?
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 如何修复编译器警告 C6386 和 C6385?
- 返回语句后的代码,没有警告
- 获取隐式转换溢出从无符号到已签名的警告
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 在未链接的部分上生成警告
- 是否有跨平台的方法可以在C++中禁用已弃用的警告?
- C printf跨平台格式无警告