Hossein
Hossein

Reputation: 26004

Is there a way to figure out the version of a compiler in code?

is it possible to get the version of compiler in code? for example using some compiler directives?
I am trying to find about the version of a compiler and then lets say if the version of Gcc or Visual C++ is C++11 compliant then compile this bit of code and if not it compile thats snippet instead

Upvotes: 2

Views: 315

Answers (3)

DUman
DUman

Reputation: 2590

If you want to know what compiler you're using, they have their own predefined macros for that, linked in other comments. But you're indicating that you are doing this in order to discover the presence of C++11 support. In that case, the correct code is

#if __cplusplus <= 199711L
    //No C++11 support
#else
    //Congratulations, C++11 support!
#endif

According to the standard, compilers are required to set that variable, and it indicates the version. See it on Bjarne's page

Upvotes: 1

alexrider
alexrider

Reputation: 4463

You can use __cplusplus macro to check if compiler supports C++11 so that it will work even on compilers you don't know about.

#if __cplusplus >= 201103L
//C++ 11 code here
#endif

16.8 Predefined macro names

1 The following macro names shall be defined by the __cplusplus The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit.

157) It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming compilers should use a value with at most five decimal digits.

Upvotes: 2

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361702

In gcc and clang, you could use __VERSION__ macro.

Upvotes: 1

Related Questions