
To prevent this fallback behaviour, the CXX_STANDARD_REQUIRED target property should be set to YES. It does not generate an error by default. There is a minor wrinkle in that if the compiler doesn’t support the specified standard, by default CMake falls back to the latest standard the compiler does support instead. It results in adding the relevant compiler and linker flags to the target to make it build with the specified C++ standard. The CXX_STANDARD target property mostly behaves as you would expect.

This variable is used as the default for the CXX_STANDARD target property, so all targets defined after the variable is set will pick up the requirement. Valid values for CMAKE_CXX_STANDARD are 98, 11 and 14, with 17 also being added in CMake 3.8 and 20 added in CMake 3.12. The simplest way to use a particular C++ standard in your project is to add the following two variable definitions before you define any targets: set(CMAKE_CXX_STANDARD 11) Happily, with features added in CMake 3.1, it is trivial to handle this in a generic way. If your project targets multiple platforms and compilers, this can be a headache to set up. With the constant evolution of C++, build systems have had to deal with the complication of selecting the relevant compiler and linker flags.
