c 14最终添加了 [[deprecated]]
属性。我想在标头文件中使用它,这些文件也需要在C 11模式下消耗。
我不介意在C 11模式下忽略弃用。
我找不到便便包装此语言功能的增强宏,因此我在每次声明之前添加了我要删除以下代码的添加:
#if __cplusplus >= 201402L
[[deprecated]]
#endif
使用Boost或其他公共库进行此清洁程序的任何建议?
注意:我主要是针对G 4.8和5.x
您是否使用CMAKE,您可以使用CMAKE模块WriteCompilerDetectionHeader
生成的预处理器指令处理[[deprecated]]
属性:
include(WriteCompilerDetectionHeader)
write_compiler_detection_header(
FILE foo_compiler_detection.h
PREFIX foo
COMPILERS GNU
FEATURES cxx_attribute_deprecated
)
我尝试了一下,然后从生成的文件中提取了与您的主要目标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
我想这是您可以为G 生成的最完整的代码。如果您需要支持其他编译器,请将它们添加到上面的CMAKE代码中的COMPILERS
行中,然后重新运行CMAKE更新生成的文件。
一旦包含在内,此代码将允许您替换原始:
#if __cplusplus >= 201402L
[[deprecated]]
#endif
with:
foo_DEPRECATED
或使用带有消息的版本:
foo_DEPRECATED_MSG("this feature is deprecated, use the new one instead")
#if __cplusplus >= 201402L
# define DEPRECATED [[deprecated]]
# define DEPRECATED_MSG(msg) [[deprecated(msg)]]
#else
# define DEPRECATED
# define DEPRECATED_MSG(msg)
#endif
用法:
class DEPRECATED_MSG("Use class Y instead") X {};