Yit*_*ikC 2 boost g++ c++11 c++14
C++ 14 终于添加了该[[deprecated]]属性。我想在头文件中使用它,这些头文件也需要在 C++ 11 模式下使用而不会窒息。
我不介意在 C++ 11 模式下是否忽略弃用。
我一直无法找到可移植地包装此语言功能的 Boost 宏,因此我在每个声明之前添加了我要弃用以下代码:
#if __cplusplus >= 201402L
[[deprecated]]
#endif
Run Code Online (Sandbox Code Playgroud)
有什么建议可以使用 Boost 或其他常用库使这个更清洁吗?
注意:我主要针对 G++ 4.8 和 5.x
您是否使用过 CMake,您可以[[deprecated]]使用 CMake 模块生成的预处理器指令处理属性WriteCompilerDetectionHeader:
include(WriteCompilerDetectionHeader)
write_compiler_detection_header(
FILE foo_compiler_detection.h
PREFIX foo
COMPILERS GNU
FEATURES cxx_attribute_deprecated
)
Run Code Online (Sandbox Code Playgroud)
我试过了,并从生成的文件中提取了与主要目标 g++ 相关的代码:
# define foo_COMPILER_IS_GNU 0
#if defined(__GNUC__)
# undef foo_COMPILER_IS_GNU
# define foo_COMPILER_IS_GNU 1
#endif
# if foo_COMPILER_IS_GNU
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 409 && __cplusplus > 201103L
# define foo_COMPILER_CXX_ATTRIBUTE_DEPRECATED 1
# else
# define foo_COMPILER_CXX_ATTRIBUTE_DEPRECATED 0
# endif
# endif
# ifndef foo_DEPRECATED
# if foo_COMPILER_CXX_ATTRIBUTE_DEPRECATED
# define foo_DEPRECATED [[deprecated]]
# define foo_DEPRECATED_MSG(MSG) [[deprecated(MSG)]]
# elif foo_COMPILER_IS_GNU
# define foo_DEPRECATED __attribute__((__deprecated__))
# define foo_DEPRECATED_MSG(MSG) __attribute__((__deprecated__(MSG)))
# else
# define foo_DEPRECATED
# define foo_DEPRECATED_MSG(MSG)
# endif
# endif
Run Code Online (Sandbox Code Playgroud)
我想这是您可以为 g++ 生成的最完整的代码。如果您需要支持其他编译器,请将它们添加到COMPILERS上述 CMake 代码中的行中,然后重新运行 CMake 以更新生成的文件。
包含后,此代码将允许您替换原始代码:
#if __cplusplus >= 201402L
[[deprecated]]
#endif
Run Code Online (Sandbox Code Playgroud)
和:
foo_DEPRECATED
Run Code Online (Sandbox Code Playgroud)
或者,使用带有消息的版本:
foo_DEPRECATED_MSG("this feature is deprecated, use the new one instead")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
664 次 |
| 最近记录: |