在 C++ 中将类/方法标记为过时或已弃用

问题描述 投票:0回答:4

有没有办法将 C++ 中的方法/类标记为过时?

在c#中你可以这样写:

[Obsolete("You shouldn't use this method anymore.")]
void foo() {}

如果重要的话,我会使用 GNU 工具链/Eclipse CDT。

c++ attributes
4个回答
9
投票

最简单的方法是使用

#define DEPRECATED
。在 GCC 上,它扩展为
__attribute__((deprecated))
,在 Visual C++ 上,它扩展为
__declspec(deprecated)
,而在没有类似功能的编译器上,它扩展为空。


8
投票

c++14 标准(或更高版本)现在提供此功能:https://en.cppreference.com/w/cpp/language/attributes/deprecated

[[deprecated]] void F();
[[deprecated("reason")]] void G();
class [[deprecated]] H { /*...*/ };

7
投票

仅使用编译器相关的编译指示:查找文档

 int old_fn () __attribute__ ((deprecated));

0
投票

我不知道您使用的 C++ 版本,但 Microsoft 的 Visual C++ 有一个“已弃用的 pragma”。 也许你的版本有类似的东西。

© www.soinside.com 2019 - 2024. All rights reserved.