3

Sun 编译器是否具有将函数标记为已弃用的符号,例如 GCC__attribute__ ((deprecated))或 MSVC __declspec(deprecated)

4

2 回答 2

2

似乎适用于任何支持的编译器的一种解决方案#warning是:

  • 将有问题的标题复制到一个新的、提升的标题名称
  • 从提升的头文件中删除不推荐使用的函数
  • 添加到旧的头文件:#warning "This header is deprecated. Please use {new header name}"
于 2009-06-02T01:07:34.953 回答
1

这将在带有“+w”标志的 sun 或带有“-Wall”标志的 gcc 上为您提供编译器警告。不幸的是,它破坏了函数的 ABI 兼容性;我还没有找到解决方法。

#define DEPRECATED char=function_is_deprecated()

inline char function_is_deprecated()
{
    return 65535;
}

void foo(int x, DEPRECATED)
{
}

int main()
{
    foo(3);
    return 0;
}

输出:

CC -o test test.cpp +w
"test.cpp", line 7: Warning: Conversion of "int" value to "char" causes truncation.
"test.cpp", line 15:     Where: While instantiating "function_is_deprecated()".
"test.cpp", line 15:     Where: Instantiated from non-template code.
1 Warning(s) detected.

使用它的方式是,当您想要声明一个已弃用的函数时,您在其参数列表的末尾添加一个逗号并写为 DEPRECATED。它在后台工作的方式是添加一个导致转换警告的默认参数(因此保留 API)。

于 2009-05-21T17:15:09.223 回答