gcc/clang "array bound is not an integer constant" in CI
A C++ fixed-size array needs a compile-time constant bound. A non-const variable, a function result, or a runtime value cannot size it, and C++ has no variable-length arrays.
What this error means
Declaring T arr[n] where n is a non-const variable fails with "array bound is not an integer constant", though it may compile as a GNU extension elsewhere.
gcc
app.cpp:5:11: error: array bound is not an integer constant before ']' token
5 | int data[n];
| ^Common causes
How to fix it
Use a constant bound or std::vector
- Make the bound constexpr if it is known at compile time.
- For runtime sizes, use std::vector instead of a fixed array.
gcc
#include <vector>
std::vector<int> data(n); // runtime-sized
constexpr int N = 16; int fixed[N]; // compile-timeHow to prevent it
- Size fixed arrays with constexpr values and reach for std::vector whenever the length is only known at runtime.
Related guides
gcc/clang "call to non-constexpr function" in constant expression in CIFix gcc/clang errors where a constant expression calls a non-constexpr function in CI - a constexpr context r…
gcc/clang "no matching function for call to" (explicit overload) in CIFix gcc/clang "no matching function for call to" caused by an explicit constructor in CI - an implicit conver…