I am starting to rewrite a significant amount of code that should work for several OS / compiler combinations. Some support C ++ 11 and others only support C ++ 98 / C ++ 03. I am looking for a way to use some of the features of C ++ 11 in code.
The primary ones that interest me are override
, noexcept
and final
. In other words, syntactically small functions that actually don't have the C ++ 98/03 equivalent. I am not trying to use shoehorn in ranges for loops, using ifdef
for example in What is the equivalent of C and 98 to reference an iterator? . This is too awkward.
My initial thought was to use a preprocessor for something like this:
#if __cplusplus > 201103L
#define OVERRIDE override
#else
#define OVERRIDE
#endif
Then my function declarations will look like this:
virtual void myFunction() OVERRIDE;
I don’t know if products like Boost have any mechanism for this, but it doesn’t matter in my case, since I won’t have access to Boost or anything like that on some OSs. It is also not possible to update the OS / compiler. I either have to do it myself or not do it at all.
Is there a better way to do this? Am I setting myself up for some unknown headache using this method? Also, should you try to use different names for macros, for example MY_OVERRIDE
, to avoid name collisions?
source
share