Constexpr Testing Finds Compiler and Standard Library Bugs Without a Board

Last updated:

A constexpr test suite, one the compiler runs while it builds your code, doubles as a probe for the compiler and standard library themselves. Cross-compile it for each toolchain you ship on and a failure is either your bug or the toolchain’s, and either way you learn it from a build log, with no board and no flashing. This post covers three such failures from my open-source CBOR decoder, constexpr-cbor: a std::string failure in the ARM GCC toolchain, a warning that contradicts itself across word sizes, and a standard library function that isn’t usable at compile time.

The cost case is short. A toolchain-specific defect found in a build log costs an engineer an afternoon. The same defect found on one customer’s hardware costs a release and a diagnosis with no repro.

What does a constexpr test suite have to do with toolchain bugs?

A compile-time test is a function the compiler evaluates itself, checked with static_assert. That evaluation is strict: reading the wrong union member or calling something that isn’t constant-evaluable is a hard error, not undefined behavior that happens to work. The technique is covered in Compile-Time Testing in C++: Prove It, Don’t Just Test It and on the compile-time testing hub.

The side effect is what matters here. The compiler’s evaluator and the library’s constexpr support are part of what you are testing, and because the check happens at compile time you only need the cross-compiler, not the target. For the ARM case that was arm-none-eabi-g++ in a Docker container, compiling with -c and never linking.

Why does moving a std::string fail in a constant expression on arm-gcc?

The decoder returns Expected<T, Error>, a stand-in for C++23’s std::expected that stores its value in a std::variant. The first version took T by value and moved it into the variant, the shape most reviewers would wave through. It passed on every desktop compiler and failed on arm-none-eabi-g++ 13.2.1, the toolchain Ubuntu packages as GNU Arm Embedded:

error: accessing 'std::__cxx11::basic_string<char>::<unnamed union>::_M_allocated_capacity'
member instead of initialized '..._M_local_buf' member in constant expression

Here is a repro with no CBOR in it. It builds a std::string from an iterator range, as the decoder does for text, and moves it into a std::variant.

template <typename T, typename E>
struct ByValue {
  std::variant<T, E> s;
  constexpr ByValue(T v) : s(std::in_place_index<0>, std::move(v)) {}
  constexpr const T& get() const { return std::get<0>(s); }
};

constexpr std::string_view src = "hello";
constexpr bool run() {
  ByValue<std::string, int> w(std::string(src.begin(), src.end()));
  return w.get() == "hello";
}
static_assert(run());

arm-none-eabi-g++ 13.2.1 rejects this at both -std=c++20 and -std=c++23, and host g++ 13.3 accepts it. I haven’t traced the cause inside libstdc++, so I won’t name one.

The workaround is a forwarding-reference constructor that builds the variant’s T straight from the caller’s argument, so no named intermediate std::string exists to be moved from.

template <typename U>
constexpr Fwd(U&& v) : s(std::in_place_index<0>, std::forward<U>(v)) {}

That compiles on the ARM toolchain and everywhere else. I prefer it to the alternatives I weighed: requiring a newer ARM toolchain (users get whatever their vendor ships) or #ifdef-ing the compiler out (it hides the problem and rots).

Which cast satisfies -Wuseless-cast on both 32-bit and 64-bit targets?

None of them. CBOR lengths arrive as uint64_t, and a std::span indexes with std::size_t. On 64-bit desktops those are the same type, so GCC’s -Wuseless-cast rightly objects to a static_cast between them, and the code left the cast out. On wasm32, where size_t is 32 bits, Clang’s -Wshorten-64-to-32 then flagged that same uncast line; the 32-bit GCC embedded builds had stayed silent.

A helper that casts only when the types differ keeps both warnings quiet:

template <typename To, typename From>
constexpr To narrow(From v) {
  if constexpr (std::is_same_v<To, From>) return v;
  else return static_cast<To>(v);
}

The call sites also record that the value was already bounds-checked, which is why narrowing is safe.

Why can’t you call std::isnan in a constexpr function?

std::isnan is only guaranteed constexpr from C++23 (P0533), and several current libc++ releases don’t mark it so even then. A decoder that must stay evaluable at C++20 can’t call it. value != value uses only floating-point comparison, which is constant-evaluable in every standard the library supports.

When is a toolchain matrix worth it?

Mine compiles every test at C++20 and C++23 on desktop GCC and Clang, plus Cortex-M4, RISC-V, ESP32 and WebAssembly cross-compilers. For a library that will only ever build on one compiler it isn’t worth the setup. For embedded work, where the vendor picks a compiler that is rarely the newest, it is.

The habit to keep: when a toolchain disagrees with you, reduce the failure to a repro with none of your code in it before deciding who is wrong. That file is also what you attach to a bug report.

Frequently asked questions

Can constexpr tests really find compiler bugs without hardware?

Yes, for bugs that appear during compile-time evaluation or compilation, like the ARM `std::string` error above. A cross-compiler on a development machine is enough. It won't find CPU-specific code generation bugs or anything that depends on real peripherals.

Is the arm-gcc std::string failure a GCC bug or a libstdc++ bug?

I don't know. What I verified is the behavior: `arm-none-eabi-g++` 13.2.1 rejects the move in a constant expression at C++20 and C++23, and host `g++` 13.3 accepts it.

Why not just turn off -Wuseless-cast?

For an application that may be right. For a library built by others with their own warning flags, a helper like `narrow` stays clean under both strict and lenient configurations.