MSVC __cplusplus macro
The __cplusplus
macro indicates the version of the C++ standard that the compiler claims to implement given the current compiler flags.
Some later language standard features like
__has_include
are available despite earlier compiler standard settings, which is a great convenience.
C++ projects regularly use the __cplusplus
macro to conditionally compile code based on the C++ standard version implemented by the compiler in use.
This allows adding new optional features, which still working with older compilers that do not support them.
Surprisingly, Visual Studio MSVC defines __cplusplus
as 199711L
by default, which is the C++98 standard.
Visual Studio 2017 15.7
added the flag
/Zc:__cplusplus
to define __cplusplus
as the correct value like other compilers.
Intel
oneAPI 2023.1 release
uniformly adds the MSVC flag /Zc:__cplusplus
.
To see the note, scroll down to the text “oneAPI 2023.1, Compiler Release 2023.1 New in this release” and click the down caret.
Added /Zc:__cplusplus as a default option during host compilation with MSVC.
In CMake, add this flags as needed by deciphering the MSVC compiler version.
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.14)
# MSVC has __cpluscplus = 199711L by default, which is C++98!
# oneAPI since 2023.1 sets __cplusplus to the true value with MSVC by auto-setting this flag.
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>")
endif()
Meson build system adds this flag automatically.