[C++] 1-1.4 visual studio 2019 obtains the compiler version

[C++] 1-1.4 visual studio 2019 obtains the compiler version

1. Background

Operating system: IDE
used by windows10 : visual studio comunity 2019

2. Code

#include <iostream>
using std::cout; 
using std::endl;
int main() {
    
    	
	// 打印__cplusplus 的值
	if (__cplusplus > 201703L) cout << "C++2a\n";
	else if (__cplusplus == 201703L) cout << "C++17\n";
	else if (__cplusplus == 201402L) cout << "C++14\n";
	else if (__cplusplus == 201103L) cout << "C++11\n";
	else if (__cplusplus == 199711L) cout << "C++98\n";
	else cout << "pre-standard C++\n";

	cout << __cplusplus << std::endl;

	cout << "Press ANY key to exit.";
	//std::cin.get();
	return (0);
} 

3. Run

My project is Project1, and its properties are configured as follows:

The menu bar of vs2019: Project (P) -> Project1 properties (my project name is Project1)
as shown in Figure 1:
Insert picture description here
Enter the Project1 properties page, select "General" under "Configuration Properties" -> "C++ Language Standard" ->" ISO C++17 standard (std:c++17)"
as shown in Figure 2:
Insert picture description here
On the Project1 property page, select "C/C++" under "Configuration Properties" -> "Command Line" -> "Other Options (D) "
Enter: /Zc:__cplusplus (note the two underscores and dashes _ _)
as shown in Figure 3:
Insert picture description here
Run as shown in Figure 4:
Insert picture description here

4. Description

When the configuration in Figure 3 above is canceled, and the "/Zc:__cplusplus" in "Other Options (D)" is deleted, run the following Figure 5:
Insert picture description here
Note:
Visual studio takes into account the compatibility of the old project, deliberately changes "__cplusplus" "Is defined as 199711, that is, "C++98";
to change "__cplusplus" to the version corresponding to the compiler, you need to configure the display according to Figure 3 above.

Guess you like

Origin blog.csdn.net/jn10010537/article/details/115037213