Skip to content

Added a `cxx_std_latest` compile feature (and other equivalents).

The MSVC has a means to specify that the latest C++ standard should be used via the /std:c++latest command line argument. It would be convenient if CMake provided a similar facility when generating build files so that users did not have to update their own CMake scripts to account for a new version of the standard.

The changes in this branch add the following recognized compile features to CMake:

  • c_std_latest
  • cxx_std_latest
  • cuda_std_latest
  • hip_std_latest (NOTE: CMake recognizes hip_std_XXX compile features, but this behavior does not seem to be documented.)

When specified for a target, CMake will use the latest version of a given language's standard which is supported by the corresponding compiler being used. This works even for compilers which do not have an equivalent to MSVC's /std:c++latest, since a change or update in compiler will require regenerating the CMake cache, anyways.

Internally, this works by adding the XXX_std_latest compile feature to the list of compile features associated with a given standard for a particular language. Each language has a per-standard set of compile features which is kept by CMake. For instance, the internal variable CMAKE_CXX23_COMPILE_FEATURES is a list of all features associated with C++23 which are supported by the current compiler. (This behavior isn't described in the CMake documentation, which only lists CMAKE_CXX_COMPILE_FEATURES as an internal variable.)

By adding the XXX_std_latest compile feature to the standard-specific feature list, the <anonymous namespace>::StandardLevelComputer::CompileFeatureStandardLevel() function (located in cmStandardLevelResolver.cxx) will be able to correctly associate it with the corresponding cmStandardLevel value. As a result, very few changes actually needed to be made on the C++ side of CMake.

The addition of these compile features warrants an update to CMake's documentation. To that end, the following pages have been updated:

  • CMAKE_C_KNOWN_FEATURES now includes the c_std_latest feature.
  • CMAKE_CXX_KNOWN_FEATURES now includes the cxx_std_latest feature.
  • CMAKE_CUDA_KNOWN_FEATURES now includes the cuda_std_latest feature.

There appears to be a precedent for not documenting the fact that CMake has a list of known and detected HIP compile features, so the hip_std_latest compile feature remains undocumented. In addition, I was not sure as to which version of CMake these additions should be attributed to. I listed them as being new to CMake version 3.30, but feel free to correct or adjust this if necessary.

Lastly, the changes in this branch include the addition or modification of the following tests:

  • RunCMake.target_compile_features (Modified): Added two simple sub-tests which verify that c_std_latest and cxx_std_latest are valid compile features when calling target_compile_features().
  • RunCMake.STDLatest (New): Verifies that the xxx_std_latest compile feature is added to the correct CMAKE_XXXYY_COMPILE_FEATURES list, where XXX is an uppercase language identifier, xxx is the corresponding lowercase language identifier, and YY is the highest supported standard level for language XXX. There are some important caveats here; see the message attached to commit 7a3b05a673200a4b5b2d94b5b2827d0530f78dcc 5dec9d695377a1174a297ffaf27112d649f6d0cc for more details.
  • CompileFeatures (Modified): Updated to account for the existence of c_std_latest and cxx_std_latest within CMAKE_C_KNOWN_FEATURES and CMAKE_CXX_KNOWN_FEATURES, respectively.

Let me know if there is anything you would like me to change, or if you have any questions.

Edited by Tyler Nichols

Merge request reports