代码之家  ›  专栏  ›  技术社区  ›  Drew Dormann

Sun C++编译器的“弃权”符号?

  •  3
  • Drew Dormann  · 技术社区  · 16 年前

    sun编译器是否有将函数标记为已弃用的符号,如gcc的符号? __attribute__ ((deprecated)) 或MSVC的 __declspec(deprecated) ?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Drew Dormann    16 年前

    似乎有一个解决方案可以解决 任何 支持的编译器 #warning 将是:

    • 将相关标题复制到新的已升级标题名称
    • 从升级的头文件中删除不推荐使用的函数
    • 添加到旧的头文件: #warning "This header is deprecated. Please use {new header name}"
        2
  •  1
  •   Joseph Garvin    16 年前

    这将在Sun上得到一个带有“+w”标志的编译器警告,或者在GCC上带有“-wall”标志的编译器警告。不幸的是,它破坏了函数的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时,在它的参数列表的末尾添加一个逗号,然后写deprecated。它在引擎盖下工作的方式是添加一个默认参数(从而保留API),从而导致转换警告。

    推荐文章