mardi 3 juillet 2018

Warn only when *calling* a deprecated method

I want to mark a pure virtual method in a base class deprecated, and get a compiler warning when someone tries to call the method (or its overrides) somewhere, but not, like it currently is, for every declaration of an override—because the method is pure virtual in the base class, all child classes must implement it to be instantiable, but that means every one of those overrides produces a (or multiple) compiler warning(s), which kinda spams the output. So I'd like to throw the warning only when the method (or one of the overrides) is being called.

Is that possible somehow without jumping through hoops*?

PS: I mark the base method as deprecated using the following macro:

#if defined(_MSC_VER)
    #define DEPRECATED __declspec(deprecated)
#elif defined(__GNUC__) || defined(__clang__)
    #define DEPRECATED __attribute__((deprecated))
#else
    #pragma message("WARNING: DEPRECATED macro not implemented for this compiler")
    #define DEPRECATED
#endif

…and I'm currently compiling in VS2015. The compiler warnings are output both if I only mark the base method deprecated and if I use the macro on both the base method and the overrides.

*) like putting a message(...) or static assert into every overrides' function definition and inlining them, or something like that... I don't think that would be worth it.

Aucun commentaire:

Enregistrer un commentaire