When working with C++ there are two versions we care about:

  • What C++ standard are we using?
  • What C++ compiler are we using?

In this article I will show how to check both of these versions.

How to Check C++ Standard Version

First, is how to check the C++ standard version.

The C++ language defines the following values corresponding

C++ Standard Version __cplusplus
C++98 199711L
C++11 201103L
C++14 201402L
C++17 201703L
C++20 202002L
C++23 202302L

The format __cplusplus is defined as a integer literal in the form YEARMONTH. For example, in C++23 __cplusplus is defined as 202302L, referring to Febraury 2023 (year=2023, month=02). This is roughly the date the committe approves the standard and it begins to go through the next stages of the ISO/IEC process.

For __cplusplus to be defined, the compiler flag /Zc:__cplusplus must be set. In Visual Studio the compiler flag can be added:

  • Configuration Properties -> C/C++ -> Command Line, add: /Zc:__cplusplus.

⚠️ If the compiler flag is not enabled, __cplusplus defaults to 199711L (C++98) or something else unexpected from actual standard being used, at least in Visual Studio.

Visual Studio can also select Preview - Features from the Latest C++ Working Draft (/std:c++latest) for the standard. However, this is not a fully implemented and __cplusplus will be defined not as expected. We can use the else statement to detect the non-standardized draft versions.

#include <iostream>
#include <string>

// Visual Studio must enable compiler flag: /Zc:__cplusplus
// otherwise __cplusplus defaults to 199711L (cpp98) or something else unexpected from actual standard
// See: Configuration Properties -> C/C++ -> Command Line: /Zc:__cplusplus
constexpr std::string static getCppVersion()
{
    std::string result;

    constexpr long pre98 = 1;
    constexpr long cpp98 = 199711L;
    constexpr long cpp11 = 201103L;
    constexpr long cpp14 = 201402L;
    constexpr long cpp17 = 201703L;
    constexpr long cpp20 = 202002L;
    constexpr long cpp23 = 202302L;

    // Warning C4984 'if constexpr' is a C++17 language extension
    if      constexpr (__cplusplus == cpp26) result = "C++26";
    else if constexpr (__cplusplus == cpp23) result = "C++23";
    else if constexpr (__cplusplus == cpp20) result = "C++20";
    else if constexpr (__cplusplus == cpp17) result = "C++17";
    else if constexpr (__cplusplus == cpp14) result = "C++14";
    else if constexpr (__cplusplus == cpp11) result = "C++11";
    else if constexpr (__cplusplus == cpp98) result = "C++98";
    else if constexpr (__cplusplus == pre98) result = "Pre-C++98";
    else                                     result = "Draft or pre-standardized or unknown C++ version";
    result += " " + std::to_string(__cplusplus);

    return result;
}

How to get compiler version

Next we can get the compiler version.

Check Microsoft Compiler Version cl.exe

Using Developer PowerShell for VS 2022 we can simply call cl.exe from the PowerShell commandline and get the version of the compiler in the first line. For example, cl.exe is 19.41.34123:

Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34123 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

Check g++ Compiler Version

We can get the g++ version by calling g++ --version. In this example the version is 11.4.0:

g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ℹ️ We can install g++ in Windows using Windows Subsystem for Linux (WSL) by calling: sudo apt-get install build-essential gdb


💻 More on the topic: