C++ versions

From snippet wiki
Jump to navigation Jump to search

How to read out the current C++ standard/specification from within a C++ program:

We have some macro defined:

__cplusplus
  • C++ pre-C++98: __cplusplus is 1.
  • C++98: __cplusplus is 199711L.
  • C++98 + TR1: This reads as C++98 and there is no way to check that I know of.
  • C++11: __cplusplus is 201103L.
  • C++14: __cplusplus is 201402L.
  • C++17: __cplusplus is 201703L.

So we might run something like this:

#include<iostream>

int main() {
    if (__cplusplus == 201703L) std::cout << "C++17" << endl;
    else if (__cplusplus == 201402L) std::cout << "C++14" << endl;
    else if (__cplusplus == 201103L) std::cout << "C++11" << endl;
    else if (__cplusplus == 199711L) std::cout << "C++98" << endl;
    else std::cout << "pre-standard C++" << endl;
}