Bug 30716 - recursive templates compilation fault
Summary: recursive templates compilation fault
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.3
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-02-06 19:10 UTC by generatorglukoff
Modified: 2007-02-07 09:55 UTC (History)
2 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description generatorglukoff 2007-02-06 19:10:12 UTC
I had tried to compile code like

template<int dim> class cls {
     cls<dim-1> c[16];
};
template<> class cls<0> {
};

int main() {
     cls<200> c;
     return 0;
}

and had error:

g++: Internal error: Segmentation fault (program cc1plus)
Please submit a full bug report.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.

I think, in this case better output maybe something like "no enough memory", "unable to compile" etc.



Information as defined in http://gcc.gnu.org/bugs.html

- the options given when GCC was configured/built

--prefix=/usr --libexecdir=/usr/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++



- the complete command line that triggers the bug

g++ bug.cpp


- the compiler output (error messages, warnings, etc.)

g++: Internal error: Segmentation fault (program cc1plus)
Please submit a full bug report.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.


- the preprocessed file (*.i*) that triggers the bug, generated by adding -save-temps to the complete compilation command, or, in the case of a bug report for the GNAT front end, a complete set of source files (see below)

Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/specs
Configured with: ../gcc-3.4.3/configure --prefix=/usr --libexecdir=/usr/lib --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --enable-languages=c,c++
Thread model: posix
gcc version 3.4.3
 /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/cc1plus -E -quiet -v -D_GNU_SOURCE bug.cpp -mtune=pentiumpro
ignoring nonexistent directory "/usr/lib/gcc/i686-pc-linux-gnu/3.4.3/../../../../i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/../../../../include/c++/3.4.3
 /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/../../../../include/c++/3.4.3/i686-pc-linux-gnu
 /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/../../../../include/c++/3.4.3/backward
 /usr/local/include
 /usr/lib/gcc/i686-pc-linux-gnu/3.4.3/include
 /usr/include
End of search list.
# 1 "bug.cpp"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "bug.cpp"
template<int dim> class cls {
     cls<dim-1> c[16];
};
template<> class cls<0> {
};


int main() {
     cls<200> c;
     return 0;
}
Comment 1 David Fang 2007-02-07 00:32:33 UTC
Ahh, exploding recursive templates...
For future reference, you could limit the recursion depth explicitly, using -ftemplate-depth-n

from a gcc-3.4.0 man page:
Set the maximum instantiation depth for template classes to n.  A limit on the template instantiation depth is needed to detect endless recursions during template class instantiation.  ANSI/ISO C++ conforming programs must not rely on a maximum depth greater than 17.

I think 42 would seem more appropriate than 17, but that is for The Committee to decide.  :)
Comment 2 Richard Biener 2007-02-07 09:55:20 UTC
note the size of class cls grows exponentially with its template parameter.