Sun的C ++编译器的“弃用”表示法?

时间:2009-05-21 15:18:22

标签: c++ deprecated suncc

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

2 个答案:

答案 0 :(得分:2)

似乎可以在支持#warning任何编译器上运行的解决方案是:

  • 将相关标题复制到新的提升标题名称
  • 从提升的头文件中删除已弃用的函数
  • 添加到旧标头文件: #warning "This header is deprecated. Please use {new header name}"

答案 1 :(得分: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),导致转换警告。