有没有办法将 C++ 中的方法/类标记为过时?
在c#中你可以这样写:
[Obsolete("You shouldn't use this method anymore.")]
void foo() {}
如果重要的话,我会使用 GNU 工具链/Eclipse CDT。
最简单的方法是使用
#define DEPRECATED
。在 GCC 上,它扩展为 __attribute__((deprecated))
,在 Visual C++ 上,它扩展为 __declspec(deprecated)
,而在没有类似功能的编译器上,它扩展为空。
c++14 标准(或更高版本)现在提供此功能:https://en.cppreference.com/w/cpp/language/attributes/deprecated
[[deprecated]] void F();
[[deprecated("reason")]] void G();
class [[deprecated]] H { /*...*/ };
仅使用编译器相关的编译指示:查找文档
int old_fn () __attribute__ ((deprecated));