Would you have used CMake, you could have handled [[deprecated]]
attribute using preprocessor directives generated with the CMake module WriteCompilerDetectionHeader
:
include(WriteCompilerDetectionHeader)
write_compiler_detection_header(
FILE foo_compiler_detection.h
PREFIX foo
COMPILERS GNU
FEATURES cxx_attribute_deprecated
)
I tried it, and extracted the code tied to your primary target g++ from the generated file:
# 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
I guess that's the most complete code you could produce for g++. If you need to support other compilers, add them to the COMPILERS
line in the CMake code above, and rerun CMake to update the generated file.
Once included, this code will allow you to replace your original:
#if __cplusplus >= 201402L
[[deprecated]]
#endif
With:
foo_DEPRECATED
Or, using the version with a message:
foo_DEPRECATED_MSG("this feature is deprecated, use the new one instead")