set(CMAKE_CXX_STANDARD) should set /Zc:__cplusplus for MSVC

It appears that doing (say) set(CMAKE_CXX_STANDARD 17) when targeting MSVC will set the /std:c++17 flag, but not (also) the MS-specific /Zc:__cplusplus flag; this is unfortunate, because MSVC explicitly sets __cplusplus to 199711L in all cases when /Zc:cplusplus is unspecified. (See https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/).

While this is arguably a bad decision on the part of MSVC, CMake should just quietly work around it by setting both of these flags, to ensure __cplusplus has an accurate value.

1 Like

It seems this has been raised on Gitlab in issue #18837:

https://gitlab.kitware.com/cmake/cmake/-/issues/18837

Quoting @brad.king’s comment in that issue:

Some other property could be added for this. As mentioned above a policy would be needed to change its default.

For now I think target_compile_options is a reasonable workaround.