Simultaneous maintenance of C ++ 98 and C ++ 11

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, noexceptand 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 ifdeffor 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?

+4
source share
2 answers

Could you do that?

#if __cplusplus > 201103L
#else
#define override
#endif

Then your function declarations will look like this:

virtual void myFunction() override;

, ?

+2

, -. , , , :

#ifdef __STDC__
void func(int x)
#else
void func(x)
    int x;
#endif
{
    ...
}

, Boost - , , .

, , Rs. MY_OVERRIDE , , , OVERRIDE .

P.S. , . , Visual Studio 2008 OVERRIDE , __cplusplus < 201103L.

+3

Source: https://habr.com/ru/post/1628038/


All Articles