简体   繁体   English

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

[英]“Deprecated” notation for Sun's C++ compiler?

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

It seems that one solution that would work on any compiler that supports #warning would be: 似乎可以在任何支持#warning编译器上运行的解决方案是:

  • Copy the header in question to a new, promoted header name 将有问题的标题复制到新的提升标题名称
  • Remove the deprecated functions from the promoted header file 从提升的头文件中删除已弃用的函数
  • Add to the old header file: #warning "This header is deprecated. Please use {new header name}" 添加到旧的头文件: #warning "This header is deprecated. Please use {new header name}"

This will get you a compiler warning on sun with the "+w" flag or on gcc with the "-Wall" flag. 这将使用“+ w”标志在sun上获得编译器警告,或在带有“-Wall”标志的gcc上获得编译器警告。 Unfortunately it breaks the ABI compatibility of the function; 不幸的是它打破了函数的ABI兼容性; I haven't discovered a way around that yet. 我还没有找到解决方法。

#define DEPRECATED char=function_is_deprecated()

inline char function_is_deprecated()
{
    return 65535;
}

void foo(int x, DEPRECATED)
{
}

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

The output: 输出:

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.

The way you use it is when you want to declare a function deprecated, you add a comma to the end of its parameter list and write DEPRECATED. 您使用它的方式是,当您要声明不推荐使用的函数时,可以在其参数列表的末尾添加逗号并写入DEPRECATED。 The way it works under the hood is it adds a default argument (thus preserving API) that causes the conversion warning. 它在底层工作的方式是它添加一个默认参数(从而保留API),导致转换警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM