简体   繁体   中英

writing code that supports new and older c++ compilers?

I have to write a code that can support newer and older compilers and i was wondering before i start is something like this possible?

#ifndef C++11 { //some code..... } 
#endif 

else 

#ifndef older C++ version { //some code......}
#endif

The standard requires C++11 conforming implementations to define a macro named __cplusplus to the value 201103L . Nonconforming compilers are recommended to use a value with at most five decimal digits. The same was true for C++03 where the value this should be defined to is 199711L .

However, not many compilers consider(ed) themselves standards compliant, and eg gcc defined this for a long time to be just 1L . Also you have to consider that it is not only the compiler version, but also the parameters to the compiler. Gcc only supports (part of) C++11 when you pass -std=c++0x or -std=gnu++0x . In these cases it will define a macro __GXX_EXPERIMENTAL_CXX0X__ .

So the most portable solution is to be unportable and have your own macro that you set when C++11 support is detected, and have some header/configure script in which you use the aforementioned things, along with possibly others for other supported compilers.

There's no simple universal macro, and for most compilers, it's not a binary "yes or no". Most compilers implement some C++11 features, but certainly not all.

MSVC simply has a single _MSC_VER macro indicating the version of the compiler, and if you know which features are supported by which version, then you can use that.

Clang has pretty comprehensive feature-specific macros, of the form _HAS_<feature> (I can't remember if that's the precise name).

If you want to know, across all compilers, whether feature X is available, you'll have to check all these different macros and determine the answer yourself. :)

In MSVS you have the macro, _MSC_VER which can help you. I don't know if there's such a standard macro.

The C++ standards committee spent a lot of effort to make sure, that any code written to the older standard is still valid in the new standard. And if you have to do without a feature on some platforms, using it on the others is a lot of work for rarely any gain. So just stick to the older version you need to support.

For the few exceptions the most reliable way is to test the compiler and define macros to choose the version you want to use. Either manually if you know your set of compilers or using something like autoconf or cmake if you don't. There are many compilers that support some C++11 features and not others, so there's little hope to find some test that would suffice without any work on your part. I believe all the features can be tested with just compiling; if they compile, they will generally also work.

Write your code to be compliant with the most recent compiler.

Any code which won't compile against an older version should be extracted into its own .cpp unit.

Then an alternative .cpp should be written for the old compiler.

For older builds select to include the older .cpp.

You don't need #defines.

See #ifdef Considered Harmful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM