Check current version of C++

To verify whether your program is being compiled and run with the C++17 standard, you can use a few different approaches:

1. Check Compiler Flags During Compilation

One way to confirm that your program is being compiled with C++17 is by checking the command used in the terminal when you build your project. If you see the -std=c++17 flag, then the C++17 standard is being used.

2. Use C++17-Specific Features in Your Code

You can also write a small piece of code that uses a C++17-specific feature. If the program compiles and runs successfully, you can be confident that C++17 is being used.

Here’s an example using C++17’s std::optional:

cppCopy code#include <iostream>
#include <optional>

int main() {
    std::optional<int> opt = 42;

    if (opt) {
        std::cout << "C++17 is enabled! The value is: " << *opt << std::endl;
    } else {
        std::cout << "C++17 is not enabled." << std::endl;
    }

    return 0;
}

If this code compiles without errors and runs correctly, it indicates that C++17 is being used.

3. Check the __cplusplus Macro

The __cplusplus macro is predefined by the compiler and indicates the C++ standard version being used. You can print its value to verify that C++17 is active.

Add the following code to your program:

Expected Output:

  • For C++17: The output should be 201703.

  • For C++11: The output would be 201103.

  • For C++14: The output would be 201402.

If the output is 201703, it confirms that your program is being compiled with the C++17 standard.

4. Inspect Build Output in VSCode

In VSCode, after you build your program, check the output terminal for the command that was executed. Look for the -std=c++17 flag in the output. If it’s present, the C++17 standard was used during compilation.

By following any of these steps, you can confirm whether your program is running with the C++17 standard.

Last updated