gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:39:13 24 4
gpt4 key购买 nike

#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].

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

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

关于c++ - 为什么g++ 5.4不能编译这段编译期素数代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42335343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com