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

And*_*zos 9 c++ linux gcc g++ c++11

以下代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

-std=gnu++11模式下在GCC 4.7中生成内部编译器错误.

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

出了什么问题?

Bas*_*tch 8

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

使用最近的g++-trunk:

gcc version 4.8.0 20121026 (experimental) [trunk revision 192860] (GCC) 
Run Code Online (Sandbox Code Playgroud)

具有以下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
Run Code Online (Sandbox Code Playgroud)

(这是在Debian/Sid/AMD64上配备i3770K intel处理器和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
Run Code Online (Sandbox Code Playgroud)

因此,模板扩展似乎需要大量内存,因此您编程是不合理的.

我不确定这会被接受为GCC错误.众所周知,C++ tenplate宏展开是图灵完备的,你刚刚碰壁.并且GCC中继确实报告了致命但可理解的错误.

这个故事的寓意可能是适当地限制(2)与你的系统和硬件兼容的限制,也许使用limitzsh builtin或ulimitbash builtin.

  • 那么请通过提交相关补丁为GCC做出贡献. (2认同)