Wednesday, January 5, 2022

Checking C++ version in code

If your code has to behave differently for different C++ versions, you can check the value of __cplusplus directive. It will return the year and month of C++ standard used in your current environment. Examples:

  • 199711L stands for Year=1997, Month = 11
  • 201103L stands for Year=2011, Month = 03
If you want to run code only if C++ version is less than C++11, you write:
#if __cplusplus < 201100L
When you use Visual Studio 2019 and print the value of __cplusplus to console, you will see 199711, although in the project properties - C/C++ - Language page it is set to C++14:
For Visual Studio to output the correct value, you have to go to Configuration Properties > C/C++ > Command Line and add /Zc:__cplusplus. Now you get the correct value of 201402.

No comments:

Post a Comment