Quantcast
Viewing latest article 23
Browse Latest Browse All 24

Answer by Marvin Nimnull for Generate the longest error message in C++

Using partial specialization the error can be made arbitrarily long:

template <int C, typename T> struct ERR { int operator()(int x) { return ERR<C-1,T>()(x); } };template <typename T> struct ERR<0,T> { int operator()(T x) { return x; } };int main(int argc, char* argv[]) { return ERR<123,void>()(123456); }

Produces 1.2 MB of error messages (inner part trimmed out) when compiled with MSVC:

longerr.cpplongerr.cpp(2): error C2860: 'void' cannot be used as a function parameter except for '(void)'longerr.cpp(2): note: the template instantiation context (the oldest one first) islongerr.cpp(3): note: see reference to class template instantiation 'ERR<123,void>' being compiledlongerr.cpp(1): note: while compiling class template member function 'int ERR<123,void>::operator ()(int)'longerr.cpp(3): note: see the first reference to 'ERR<123,void>::operator ()' in 'main'longerr.cpp(1): note: see reference to class template instantiation 'ERR<122,T>' being compiled        with        [            T=void        ]longerr.cpp(1): note: while compiling class template member function 'int ERR<122,T>::operator ()(int)'        with        [            T=void        ].........longerr.cpp(1): note: see the first reference to 'ERR<122,T>::operator ()' in 'ERR<123,void>::operator ()'        with        [            T=void        ]longerr.cpp(3): note: see the first reference to 'ERR<123,void>::operator ()' in 'main'longerr.cpp(1): note: see reference to class template instantiation 'ERR<0,T>' being compiled        with        [            T=void        ]

Instead of 123 a larger constant can be used (up to the compiler limit which is platform dependent).


Viewing latest article 23
Browse Latest Browse All 24

Trending Articles