C++11:g++-4.7 内部编译器错误 [英] C++11: g++-4.7 internal compiler error

查看:21
本文介绍了C++11:g++-4.7 内部编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

#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 中产生内部编译器错误.

Produces an internal compiler error in GCC 4.7 in -std=gnu++11 mode.

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

出了什么问题?

推荐答案

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

It seems that your program requires an unreasonable amount of memory (perhaps because of too many template expansions).

使用最近的g++-trunk:

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

具有以下 zsh 限制:

with the following zsh limits:

   % 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

(在 Debian/Sid/AMD64 上使用 i3770K 英特尔处理器和 16Gb RAM)

(this on Debian/Sid/AMD64 with i3770K intel processor & 16Gb RAM)

我得到:

  % 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

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

So it seems that template expansion requires so much memory that you program is not reasonable.

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

I'm not sure if this will be accepted as a GCC bug. The macro expansion for C++ templates is known to be Turing complete and you just hit the wall. And the GCC trunk does report a fatal, but understandable error.

故事的寓意可能是 setrlimit(2) 适当地(限制与您的系统和硬件兼容),可能使用 limit zsh 内置或 ulimit bash 内置.

The moral of the story might be to setrlimit(2) appropriately (with limits compatible with your system and hardware), perhaps using limit zsh built-in or ulimit bash built-in.

这篇关于C++11:g++-4.7 内部编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆