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.

2 Likes

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.

I don’t believe it is a reasonable workaround, because it requires the CMakeLists to be specifically aware of this issue with MSVC. The CMakeLists author’s intent is expressed clearly when a standard is set. In fact, @brad.king pretty much said so on the issue page.

1 Like