Get Version of C++ Standard Used to Compile C++ Code

Get Version of C++ Standard Used to Compile C++ Code

C++ is known for constantly evolving, with each new standard bringing in innovative features and improvements. Understanding the version of the C++ standard used to compile the code is important for ensuring compatibility and leveraging the latest language features. This tutorial demonstrates how to get the version of C++ standard used to compile C++ code.

The __cplusplus is a predefined macro in C++ compilers (e.g. g++ on Linux) that provides information about the version of the C++ standard being used during compilation.

#include <iostream>

int main()
{
    std::cout << __cplusplus << std::endl;

    return 0;
}

The value assigned to __cplusplus corresponds to the year and month of the C++ standard. For example:

  • 199711L (C++98)
  • 201103L (C++11)
  • 201402L (C++14)
  • 201703L (C++17)
  • 202002L (C++20)
  • 202302L (C++23)
  • other number (pre-standard C++)

For example, if the output is 201703L, it indicates that the code was compiled using the C++17 standard.

It's important to note that not all C++ compilers set the __cplusplus macro consistently or correctly. While many modern compilers adhere to the standard and provide accurate values, there may be instances where certain compilers or specific configurations might not handle this macro as expected.

Leave a Comment

Cancel reply

Your email address will not be published.