2

I often hear about "C++ sources take a lot of time and memory to compile".

I also hear about that C++ template is Turing complete, so it may suffer from Halting problem.

I have also built a C++ project that costs 8 GiB of memory and 2 hours of time.

So, the question is: Is there a C++ code that compiles for infinite time?

(Nested includes or nested templates are detectable so they should not count.)

Related question: Is there a C++ code that compiles with infinite memory? (I separated them since I expect different answer.)

Community
  • 1
  • 1
Star Brilliant
  • 2,916
  • 24
  • 32
  • 3
    How would anybody know - but I guess you can wait until it finishes? – Ed Heal Jan 03 '15 at 11:41
  • Theoretically it's probably possible to create some source that for a generic and pure C++ compiler will take infinite time to compile. Practically and with a modern compiler? Probably not possible. – Some programmer dude Jan 03 '15 at 11:42
  • Depth limits result in compilation time being finite, but tt's probably quite easy to make a program that compiles longer than lifetime of universe. – zch Jan 03 '15 at 11:44
  • 1
    By definition, if it requires infinite time to compile then compilation will not complete in finite time. – Elliott Frisch Jan 03 '15 at 11:44
  • 2
    Since C++ template itself if Turing complete, it suffers from the "Halting Problem" (Google it). So is there a valid C++ program that satisfy the conditions that make the compiler never halt? @EdHeal – Star Brilliant Jan 03 '15 at 11:44
  • @EdHeal How do I know that `while ( true ) { }` never stops? Because that's well-defined. You don't necessarily need to run stuff to know how long it will run. – stefan Jan 03 '15 at 11:44
  • It was an attempt at some humour for a silly question. BTW A computer will eventually stop - hardware failure for a start – Ed Heal Jan 03 '15 at 11:47
  • 1
    It is true that your two questions are separate questions, but there is an implication between them: it takes an infinite amount of time to allocate infinite memory out of finite chunks (or conversely if you prefer, if the compilation terminates in finite time, look at how much memory was used: that's the amount of memory required for compilation). So you could have started with just the one question. – Pascal Cuoq Jan 03 '15 at 11:49
  • This question appears to be off-topic because it is not about a practical programming problem the OP faces. – Pascal Cuoq Jan 03 '15 at 11:50
  • @PascalCuoq Maybe I should move the question to another tag? – Star Brilliant Jan 03 '15 at 11:51
  • I'd guess this question is equivalent to "Can we create a C++ template that loops endlessly without requiring infinite recursion depth?" which I'd naively answer with "No". – filmor Jan 03 '15 at 11:52
  • "(Nested includes or nested templates are detectable so they should not count.)" -- Anything else you can think of is also detectable. The halting problem says there is no program that can detect all programs that don't halt. However, for every program A that doesn't halt, there is a program B that can detect program A as one that doesn't halt. The only way you can get reasonable answers is if you lift this restriction: allow answers if *any* compiler would never stop. –  Jan 03 '15 at 12:26
  • "A code" is not a thing. – Griwes Jan 03 '15 at 22:38

1 Answers1

9

theoretically this would compile for 'infinite' time because the template expansion is infinitely recursive:

template <size_t N>
struct eat
{
    static constexpr size_t value = eat<N+1>::value;
};

However, all the compilers I have used defend against this kind of code, emitting an error much like this:

/Users/richardh/Documents/dev/Scratchpad/tryit/tryit/words.cpp:136:37: fatal error: recursive template instantiation exceeded maximum depth of 256
    static constexpr size_t value = eat<N+1>::value;
                                    ^
/Users/richardh/Documents/dev/Scratchpad/tryit/tryit/words.cpp:136:37: note: in instantiation of template class 'eat<257>' requested here
    static constexpr size_t value = eat<N+1>::value;
                                    ^
/Users/richardh/Documents/dev/Scratchpad/tryit/tryit/words.cpp:136:37: note: in instantiation of template class 'eat<256>' requested here
    static constexpr size_t value = eat<N+1>::value;
                                    ^
/Users/richardh/Documents/dev/Scratchpad/tryit/tryit/words.cpp:136:37: note: in instantiation of template class 'eat<255>' requested here
    static constexpr size_t value = eat<N+1>::value;
                                    ^
/Users/richardh/Documents/dev/Scratchpad/tryit/tryit/words.cpp:136:37: note: in instantiation of template class 'eat<254>' requested here
    static constexpr size_t value = eat<N+1>::value;

...etc

EDIT: ok, I think this one really is infinite:

template <class T>
struct eat2
{
    using inner = eat2<eat2<T>>;
    static constexpr int value() {
        return inner::value();
    }
};

int main()
{
    eat2<int> e;
    cout << e.value() << endl;
    return 0;
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • This won't be infinite even if you'd increase the recursion depth (for me it works up to 1139) as `size_t` has a finite set of values. – filmor Jan 03 '15 at 13:24
  • 1
    Fair enough, it will (theoretically) stop compiling once N rolls over after 2^64 (on my compiler) iterations. – Richard Hodges Jan 03 '15 at 13:26
  • @filmor It's trivial to expand that code to add a new template parameter with a fresh counter once the value reaches the maximum value of size_t. – stefan Jan 03 '15 at 13:30
  • I have already read another example like this http://stackoverflow.com/a/6079650/2557927 . And I appreciate for your answer. – Star Brilliant Jan 03 '15 at 13:33