gpt4 book ai didi

C++11: g++-4.7 内部编译器错误

转载 作者:IT王子 更新时间:2023-10-29 00:13:20 24 4
gpt4 key购买 nike

以下代码:

#include <iostream>
#include <array>
using namespace std;

constexpr int N = 1000000;
constexpr int f(int x) { return x*2; }

typedef array<int, N> A;

template<int... i> struct F { static constexpr A f() { return A{{ ::f(i)... }}; } };

template<class A, class B> struct C {};
template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i..., (sizeof...(i)+j)...>
{
using T = F<i..., (sizeof...(i)+j)...>;
};

template<int n> struct S : C<typename S<n/2>::T, typename S<n-n/2>::T> {};
template<> struct S<1> : F<0> { using T = F<0>; };

constexpr auto X = S<N>::f();

int main()
{
cout << X[3] << endl;
}

-std=gnu++11 模式下的 GCC 4.7 中产生内部编译器错误。

$ g++ -std=gnu++11 test.cpp
g++-4.7.real: internal compiler error: Killed (program cc1plus)

出了什么问题?

最佳答案

您的程序似乎需要不合理的内存量(可能是因为模板扩展太多)。

使用最近的 g++-trunk :

gcc version 4.8.0 20121026 (experimental) [trunk revision 192860] (GCC) 

具有以下 zsh 限制:

   % limit          
cputime unlimited
filesize unlimited
datasize 15000MB
stacksize 8MB
coredumpsize 400MB
memoryuse 15000MB
maxproc 128166
descriptors 1024
memorylocked 64kB
addressspace 16000MB
maxfilelocks unlimited
sigpending 128166
msgqueue 819200
nice 0
rt_priority 0
rt_time unlimited

(在配备 i3770K 英特尔处理器和 16Gb RAM 的 Debian/Sid/AMD64 上运行)

我得到:

  % time g++-trunk -std=gnu++11 andrew.cc -o andrew
virtual memory exhausted: Cannot allocate memory
g++-trunk -std=gnu++11 andrew.cc -o andrew :
108.25s user 3.28s system 89% cpu 2:03.98 total

所以看起来模板扩展需要那么多内存,你编程是不合理的。

我不确定这是否会被接受为 GCC 错误。众所周知,C++ 模板的宏扩展是图灵完备的,而您只是碰壁了。 GCC 主干确实报告了一个致命但可以理解的错误。

这个故事的寓意可能是setrlimit(2)适本地(限制与您的系统和硬件兼容),也许使用 limit zsh built-in 或 ulimit bash built-in。

关于C++11: g++-4.7 内部编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103396/

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