I know that the following snippet of source code is invalid with C ++ code, but just to illustrate my question:
#include <iostream>
namespace mylib {
using deprecated = [[deprecated]];
}
[[mylib::deprecated]] void deprecated_function() {
std::cout << "Calling deprecated function!" << std::endl;
}
int main(void) {
deprecated_function();
return 0;
}
Is there any way to achieve this? The goal is to discard #defineand use attribute specifiers exclusively - excluding compiler protection and verification, because it will certainly require some degree of preprocessing-fu proficiency :) (but then predef is your friend ).
For instance:
namespace mylib {
#if __cplusplus > 201402L
using deprecated = [[deprecated]]
#elif defined(__clang)
using deprecated = [[clang::deprecated]]
#elif defined(__GNUC__)
using deprecated = [[gnu::deprecated]]
#else
using deprecated = [[completely_invalid_identifier__]];
#endif
}
[[mylib::deprecated]] void deprecated_function();
My current solution is to use #definewhere the attribute will be, for example:
#if __cplusplus > 201402L
#define mylib_DEPRECATED [[deprecated]]
#elif defined(__clang)
#define mylib_DEPRECATED [[clang::deprecated]]
#elif defined(__GNUC__)
#define mylib_DEPRECATED [[gnu::deprecated]]
#else
#define mylib_DEPRECATED
#endif
mylib_DEPRECATED void deprecated_function();
, , , ? (, , GCC, ).