Having a constexpr constructor on a class with a large array member will result in excessive memory usage. For example, I wasn't able to compile the following program using -std=c++11 on a machine with 4GiB of RAM: #include <bitset> int main() { std::bitset<2147483648> bs; } This happens because the constructor of bitset is constexpr, so it will try to initialize the large member array at compile time, which will create at least one ast tree node for every element in the array. (gcc/cp/constexpr.c:1874)
Confirmed. Unbounded constexpr can eat up a lot of memory. Here's a smaller test case: $ (cat xx.cpp && ulimit -v $(expr 4096 \* 16) && /build/gcc-trunk-svn/gcc/xg++ -B /build/gcc-trunk-svn/gcc -DNBITS=10000 -S -Wall -Wextra xx.cpp); echo $? template <int N> struct S { int a [N / (8 * sizeof (int))]; constexpr S (): a () { } }; S<NBITS> s; 0 $ (ulimit -v $(expr 4096 \* 16) && /build/gcc-trunk-svn/gcc/xg++ -B /build/gcc-trunk-svn/gcc -DNBITS=10000000 -S -Wall -Wextra xx.cpp); echo $?virtual memory exhausted: Cannot allocate memory 1
I believe this can actually be considered a duplicate of bug 56671. *** This bug has been marked as a duplicate of bug 56671 ***