Posts

Showing posts from February, 2026

Both gcc and clang generate strange/inefficient code

I ran into some surprisingly weird output of both Clang and gcc on a simple code snippet, and I thought I'd share it. Consider the following C++ function which, in a roundabout way, checks whether an std::array passed as an argument only contains zeros: #include <array> static constexpr int arraySize = 1; bool isAllZeros (const std::array<int, arraySize> &array) { std::array<int, arraySize> allZeros {}; return array == allZeros; } In case you're wondering, the reason why this is correct is that initializing an array with { } results in each element of the array being value initialized , or in other words set to zero. What happens if we compile this with gcc? Using godbolt and the latest gcc version (15.2), with optimizations on ("-O3") we get the following x86-64 Assembly code: isAllZeros(std::array&ltint, 1ul&gt const&): mov eax, DWORD PTR [rdi] test eax, eax sete al ret Alre...