C++98 (211 bytes) g++-5 (Ubuntu 5.2.1-23ubuntu1~12.04) 5.2.1 0151031
I wanted to see how well I could do in C++ without using the preprocessor at all. This program produces 2,139,390,572 bytes of output, most of which is a single error message.
template<int i,class S,class T>struct R{typedef R<i,typename R<i-1,S,S>::D,typename R<i-1,S,S>::D>D;};template<class S,class T>struct R<0,S,T>{typedef S D;};void f(){R<27,float,R<24,int*const*,int>::D>::D&E=4;}
me@Basement:~/src/junk$ ls -l a.C
-rw-rw-r-- 1 me me 211 Apr 27 21:44 a.C
me@Basement:~/src/junk$ g++-5 a.C -fmax-errors=1 2>a.C.errors.txt
me@Basement:~/src/junk$ ls -l a.C.errors.txt
-rw-rw-r-- 1 me me 2139390572 Apr 27 22:01 a.C.errors.txt
Ungolfed:
template <int i, class S, class T>
struct R {
typedef R<i, typename R<i-1,S,S>::D, typename R<i-1,S,S>::D> D;
};
template <class S, class T>
struct R<0, S, T> {
typedef S D;
};
void f() {
R<27, float, R<24, int*const*, int>::D>::D &E = 4;
}
This program works by defining a recursive struct template R which holds a typedef D containing two copies of R. This results in an type name which grows exponentially, which is printed out in full in the error message. Unfortunately, g++ seems to choke while attempting to print an error message longer than (1<<31) bytes. 2,139,390,572 bytes was the closest I could get to the limit without going over. I'm curious if anyone can adjust the recursion limits and parameter types 27, float, 24, int*const*
to get closer to the limit (or find a compiler which can print an even longer error message).
Excerpts from the error message:
a.C: In function ‘void f()’:
a.C:1:208: error: invalid initialization of non-const reference of type
‘R<27, float, R<24, R<23, R<22, R<21, R<20, R<19, R<18, R<17, R<16, R<15,
R<14, R<13, R<12, R<11, R<10, R<9, R<8, R<7, R<6, R<5, R<4, R<3, R<2, R<1,
int* const*, int* const*>, R<1, int* const*, int* const*> >, R<2, R<1, int*
const*, int* const*>, R<1, int* const*, int* const*> > >, R<3, R<2, R<1,
int* const*, int* const*>, R<1, int* const*, int* const*> >, R<2, R<1, int*
const*, int* const*>, R<1, int* const*, int* const*> > > >, R<4, R<3, R<2,
R<1, int* const*, int* const*>, R<1,
...
int* const*, int* const*> > > > > > > > > > > > > > > > > > > > > > > >
>::D& {aka R<27, R<26, R<25, R<24, R<23, R<22, R<21, R<20, R<19, R<18,
R<17, R<16, R<15, R<14, R<13, R<12, R<11, R<10, R<9, R<8, R<7, R<6, R<5,
R<4, R<3, R<2, R<1, float, float>, R<1, float, float> >, R<2, R<1, float,
float>, R<1, float, float> > >, R<3, R<2, R<1, float, float>, R<1, float,
float> >, R<2, R<1, float, float>, R<1, float, float> > > >, R<4,
...
, R<1, float, float>, R<1, float, float> > >, R<3, R<2, R<1, float, float>,
R<1, float, float> >, R<2, R<1, float, float>, R<1, float, float> > > > > >
> > > > > > > > > > > > > > > > > > > > >&}’ from an rvalue of type
‘int’
template<int i,class S,class T>struct R{typedef R<i,typename
R<i-1,S,S>::D,typename R<i-1,S,S>::D>D;};template<class S,class T>struct
R<0,S,T>{typedef S D;};void
f(){R<27,float,R<24,int*const*,int>::D>::D&E=4;}
^
compilation terminated due to -fmax-errors=1.
2,139,390,572 bytes / 211 bytes = 10,139,291.8
gcc -Wall -pedantic
is very different from plain ol`gcc
is different fromtcc
is presumably different from some other c compiler. \$\endgroup\$