Keywords: Visual Studio | C++17 | Compilation Standard | Project Configuration | Command Line Parameters
Abstract: This article provides a comprehensive guide on switching from C++14 to C++17 compilation standard in Microsoft Visual Studio. It covers both project property settings and command-line options for configuring the /std:c++17 compiler flag. The content explores version-specific support differences across Visual Studio releases, including C++20 and latest draft standards, with practical code examples demonstrating C++17 features.
Configuring C++17 Compilation Standard in Visual Studio
Enabling the C++17 compilation standard in Microsoft Visual Studio is essential for utilizing modern C++ features. Since Visual Studio 2017 version 15.3, Microsoft has provided full C++17 standard support, which developers can configure through intuitive graphical interfaces or command-line parameters.
Project Property Configuration Method
For most Visual Studio projects, the most convenient configuration method is through the project properties dialog. The specific navigation path is: Project > Properties > C/C++ > Language > C++ Language Standard. In this dropdown menu, developers can see multiple standard options:
- ISO C++14 Standard - corresponding to command-line option
/std:c++14 - ISO C++17 Standard - corresponding to command-line option
/std:c++17 - ISO C++20 Standard - corresponding to command-line option
/std:c++20(Visual Studio 2022 and later) - Latest Draft Standard - corresponding to command-line option
/std:c++latest
After selecting the "ISO C++17 Standard" option, Visual Studio automatically configures the appropriate compilation parameters for the project, ensuring the compiler enables the complete C++17 feature set.
Command-Line Parameter Configuration
For scenarios requiring fine-grained compilation control or command-line builds, developers can directly specify the /std:c++17 option in compiler parameters. Here is a typical compilation command example:
cl /std:c++17 /EHsc main.cpp
This command uses the Microsoft C++ compiler to compile the main.cpp file while enabling both C++17 standard and C++ exception handling. In actual projects, this parameter can be integrated into build scripts or continuous integration systems.
C++17 New Features Example
After enabling the C++17 standard, developers can utilize numerous new features to improve code quality and development efficiency. Here is an example demonstrating structured binding:
#include <iostream>
#include <tuple>
#include <map>
void demonstrate_structured_binding() {
std::map<std::string, int> population = {
{"Beijing", 21540000},
{"Shanghai", 24280000},
{"Guangzhou", 14040000}
};
for (const auto& [city, count] : population) {
std::cout << city << ": " << count << " people" << std::endl;
}
}
int main() {
demonstrate_structured_binding();
return 0;
}
This example showcases C++17's structured binding feature, which allows direct unpacking of key-value pairs when iterating through associative containers, making code more concise and readable.
Version Compatibility Considerations
Different versions of Visual Studio vary in their support for C++ standards. Visual Studio 2017 and later versions fully support the C++17 standard, while Visual Studio 2015 supports some C++17 features but requires enabling through the /std:c++latest option.
For projects requiring backward compatibility, conditional compilation can ensure code correctness across different standards:
#if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
// C++17 specific code
std::optional<int> maybe_value = 42;
#else
// Fallback for older standards
int* maybe_value = new int(42);
#endif
Preprocessor Macro Detection
To accurately detect the currently active language standard, Visual Studio provides the _MSVC_LANG preprocessor macro. The macro's value corresponds to different C++ standard years:
- 201402L - C++14 standard
- 201703L - C++17 standard
- 202002L - C++20 standard
It's important to note that the traditional __cplusplus macro does not reflect the actual compilation standard by default, unless the /Zc:__cplusplus compiler option is explicitly enabled.
Advanced Configuration Options
Beyond basic language standard selection, Visual Studio provides additional related configuration options:
/permissive-- Enables strict standard conformance mode/Zc:__cplusplus- Updates the__cplusplusmacro to reflect the actual standard/std:c++latest- Enables the latest experimental features
These options can be combined to meet specific requirements of different projects. For example, projects requiring strict standard conformance can use both /std:c++17 and /permissive- options simultaneously.
Practical Application Recommendations
In actual development, it's recommended to choose the appropriate C++ standard based on project requirements and team technology stack:
- For new projects, directly use C++17 or higher standards
- For existing projects, gradually migrate to new standards using conditional compilation to ensure smooth transition
- In team collaboration, ensure all developers use the same version of Visual Studio and compilation standard
- In continuous integration environments, explicitly specify compilation standards to avoid issues caused by environmental differences
By properly configuring C++ compilation standards, developers can fully leverage modern C++ language features to write safer, more efficient, and more maintainable code.