__attribute__((deprecated(message)))可用于管理废弃API函数或者变量,同时保留其兼容性
#include <iostream>
#define MY_DEPR(message) __attribute__((deprecated(message)))
MY_DEPR("Please use test_fun2() instead.")
int test_fun(){
return 0;
}
int test_fun_2(){
return 0;
}
MY_DEPR("Please use class B instead.")
class A{};
class B{};
int main(int argc, char**argv){
test_fun();
A a;
return 0;
}
result(输出一些警告信息,并提示需要更换的API或者类型):
deprecated.cpp:17:7: warning: attribute ignored in declaration of ‘class A’ [-Wattributes]
class A{};
^
deprecated.cpp:17:7: note: attribute for ‘class A’ must follow the ‘class’ keyword
deprecated.cpp: In function ‘int main(int, char**)’:
deprecated.cpp:20:14: warning: ‘int test_fun()’ is deprecated: Please use test_fun2() instead. [-Wdeprecated-declarations]
test_fun();
^
deprecated.cpp:6:5: note: declared here
int test_fun(){
扩展:
// 在opencv中可以这样使用
#if defined __GNUC__
#define __CV_GPU_DEPR_BEFORE__
#define __CV_GPU_DEPR_AFTER__ __attribute__ ((deprecated))
#elif defined(__MSVC__) //|| defined(__CUDACC__)
#pragma deprecated(DevMem2D_)
#define __CV_GPU_DEPR_BEFORE__ __declspec(deprecated)
#define __CV_GPU_DEPR_AFTER__
#else
#define __CV_GPU_DEPR_BEFORE__
#define __CV_GPU_DEPR_AFTER__
#endif