함수 앞에 [[deprecated]]
를 붙이거나 변수 선언 앞에 __attribute__((deprecated))
를 붙이면 컴파일 시에 경고를 출력한다.
경고 메시지를 직접 지정하는 방법도 있다.
아래 예제를 참고할 것
#include <iostream>
using namespace std;
[[deprecated]]
void test1(void)
{
}
[[deprecated("replaced by ...")]]
void test2(void)
{
}
int main(void)
{
__attribute__((deprecated)) int a = 3;
__attribute__((deprecated("deprecated for ..."))) int b = 4;
cout << a << endl;
cout << b << endl;
test1();
test2();
return 0;
}
deprecated_attribute.cc: In function ‘int main()’:
deprecated_attribute.cc:22:13: warning: ‘a’ is deprecated [-Wdeprecated-declarations]
22 | cout << a << endl;
| ^
deprecated_attribute.cc:19:37: note: declared here
19 | attribute((deprecated)) int a = 3;
| ^
deprecated_attribute.cc:23:13: warning: ‘b’ is deprecated: deprecated for ... [-Wdeprecated-declarations]
23 | cout << b << endl;
| ^
deprecated_attribute.cc:20:59: note: declared here
20 | attribute((deprecated("deprecated for ..."))) int b = 4;
| ^
deprecated_attribute.cc:25:11: warning: ‘void test1()’ is deprecated [-Wdeprecated-declarations]
25 | test1();
| ^
deprecated_attribute.cc:6:6: note: declared here
6 | void test1(void)
| ^~~~~
deprecated_attribute.cc:25:11: warning: ‘void test1()’ is deprecated [-Wdeprecated-declarations]
25 | test1();
| ^
deprecated_attribute.cc:6:6: note: declared here
6 | void test1(void)
| ^~~~~
deprecated_attribute.cc:26:11: warning: ‘void test2()’ is deprecated: replaced by ... [-Wdeprecated-declarations]
26 | test2();
| ^
deprecated_attribute.cc:12:6: note: declared here
12 | void test2(void)
| ^~~~~
deprecated_attribute.cc:26:11: warning: ‘void test2()’ is deprecated: replaced by ... [-Wdeprecated-declarations]
26 | test2();
| ^
deprecated_attribute.cc:12:6: note: declared here
12 | void test2(void)
| ^~~~~
3
4