为什么g++ 5.4不能编译这段编译期素数代码?

Why can g++ 5.4 not compile this compile-time prime number code?

#include<iostream>
using namespace std;

template<int N> class Prime
{ // generate N prime numbers at compile time
public:
    unsigned int arr[N]{};
    constexpr Prime() {
        int k=0;
        for(unsigned int i=2; k<N; i++) {
            bool isPrime = true;
            for(int j=0; j<k; j++) {
                if(arr[j] > i/2) break;
                if(i % arr[j] == 0) {
                    isPrime = false;
                    break;
                }
            }
            if(isPrime) arr[k++] = i;
        }
    }
};
int main()
{
    Prime<50000> prime; // if 50000->5000, ok
    for(auto& a : prime.arr) cout << a << ' ';
}

G++ 无法编译此代码。它花了很长时间尝试编译,使用大量内存,最后崩溃了。

如果我将数字 50000 变小,或者去掉 constexpr,它会编译。但是我想使用更大的数组来节省时间。

任何想法将不胜感激。

这是实施质量 (QoI) 问题。来自 draft Standard

Annex B (informative) Implementation quantities [implimits]

1 Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

2 The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

(2.38) — Recursive constexpr function invocations [512].

(2.39) — Full-expressions evaluated within a core constant expression [1 048 576].

您的算法超过了 full-expressions 在核心常量表达式中求值的限制。请注意,gcc 确实超出了最低要求,因为您的循环缩放为 1/2 * N^2,而 gcc 将其编译为 N = 5,000。我一直无法找到 gcc 的记录硬限制。不幸的是,与具有 -fconstexpr-steps 的 clang 不同,您不能覆盖 gcc 的 constexpr 评估次数。

结论:向 gcc 提交性能报告或使用 clang(您的示例编译)。