Huge memory consumption during compilation when passing constexpr by const reference

Viewed 83

Compilation of the following code with g++ consumes a surprisingly big amount of memory (over 5gb).

#include <iostream>
#define N 100000

struct A {
    int tab[N];
    constexpr A(): tab{} {
        for (size_t i = 0; i < N; ++i) tab[i] = i;
    }
};

struct B {
    int tab[N];
    constexpr B(const A& a): tab{} {
        for (size_t i = 0; i < N; ++i) tab[i] = a.tab[N-1-i] * 2;
    }
};

constexpr A a;
constexpr B b(a);
 
int main(){ std::cout << b.tab[N-5]; }

It seems that it is caused by passing a const A& in B's constexpr constructor -- passing by value fixes the problem. What is happening there?

g++ version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0. It was the same on some older version too.

This problem doesn't occur when compiling with clang-10.

1 Answers
Related