Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8746973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:16:49+00:00 2026-06-13T12:16:49+00:00

The following code: #include <iostream> #include <array> using namespace std; constexpr int N =

  • 0

The following code:

#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;
}

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)

What is going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:16:49+00:00Added an answer on June 13, 2026 at 12:16 pm

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

    Using a recent g++-trunk :

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

    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
    

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

    I am getting:

      % 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.

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code: #include <iostream> using namespace std; struct stud{ int roll;
The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
Consider the following code: #include <iostream> using namespace std; class Test { static int
I’ve got the following code: #include <iostream> using namespace std; int main() { char*
following code for partition #include<iostream> using namespace std; #define maxn 10000 int x[maxn]; void
i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.