Skip to content
BETAGuardrails are in beta. The library, the configuration format and the CLI command can still change.

Guardrails / cpp

C++ standard is declared

cpp/standard-declared@v1

A CMake build that enables C++ declares the standard it compiles against, and that standard is no older than the configured floor.

Idcpp/standard-declared
Versionv1
Categorycpp
Default severitywarning
Interpreterpython3
Timeout30 seconds
Violations tolerated0
Collectscpp

Collectors

This guardrail gathers nothing itself. It depends on the collectors below, which the CLI runs once per build before any check, and reads what they found out of GUARDRAIL_FACTS. A collector that collects nothing skips this guardrail rather than failing it.

CollectorGathersInputs it is given
cppThe C or C++ build in the project directory: which build system and package manager it uses, the C++ standard and CMake version it requires, the packages it declares and which of them are pinned, and the packages it expects the machine to carry. CMakeLists.txt is a script, so what it declares conditionally is not seen and scanned says so.projectDir

The inputs above are this guardrail's own, passed straight through. Configuring one in buildnote.json changes what is collected, and two guardrails configured the same way share the one collection.

Configuration

json
{
  "guardrails": {
      "failOn": "error",
      "comment": true,
      "checks": [
          {
              "use": "cpp/standard-declared@v1",
              "severity": "warning",
              "with": {
                  "projectDir": ".",
                  "minStandard": "17"
              },
              "exemptions": []
          }
      ]
  }
}

Inputs

InputDescriptionDefaultEnvironment variable
projectDirDirectory holding the project, relative to the directory the CLI runs in..GUARDRAIL_INPUT_PROJECTDIR
minStandardOldest C++ standard accepted, written as the number CMake takes: 11, 14, 17, 20 or 23. A declared standard below this is a violation.17GUARDRAIL_INPUT_MINSTANDARD

How to fix

Name the standard the build compiles against in CMakeLists.txt:

cmake
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Only CMAKE_CXX_STANDARD in a CMakeLists.txt is read, so a standard reached for through target_compile_features alone is not seen and the build reads as declaring none. A checkout built by Meson or Bazel declares its standard somewhere nothing here reads, so the check skips rather than guessing. With none declared the compiler's own default decides, so the same commit compiles as C++14 under one toolchain and C++17 under the next, and a feature that built on a laptop fails on the runner.

More in cpp

  • cpp/cmake-minimum-floor. A CMake build names the CMake version whose policy defaults it configures under, and that version is no older than the configured floor.
  • cpp/package-manager-declared. The checkout declares its dependencies through vcpkg or Conan, so it names the versions it builds against rather than taking whatever the machine carries.
  • cpp/standard-required. A CMake build that enables C++ requires the standard it declares, so a compiler that does not offer it fails rather than falling back to an older one.

All 4 cpp guardrails

Buildnote Limited
Registered in England and Wales, Reg: 16140412