How to determine the version of the C++ standard used by the compiler?

C++Server Side ProgrammingProgramming

Sometimes we need to know that, what is the current C++ standard. To get this kind of information, we can use the macro called __cplusplus. For different standards, the value of this will be like below.

Standard__cplusplus output
C++ pre C++98
1
C++98
199711L
C++98 + TR1
This cannot be checked, this will be marked as C++98
C++11
201103L
C++14
201402L
C++17
201703L

Example

#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;
}

Output

C++98
raja
Published on 03-May-2019 10:35:37
Advertisements