简体   繁体   English

是否存在可以无限期编译的C ++代码?

[英]Is there a C++ code that compiles for infinite time?

I often hear about "C++ sources take a lot of time and memory to compile". 我经常听到有关“ C ++源代码编译需要花费大量时间和内存”的消息。

I also hear about that C++ template is Turing complete, so it may suffer from Halting problem . 我也听说C ++模板已经完成Turing,所以可能会遇到Halting问题

I have also built a C++ project that costs 8 GiB of memory and 2 hours of time. 我还建立了一个C ++项目,该项目花费8 GiB的内存和2个小时的时间。

So, the question is: Is there a C++ code that compiles for infinite time? 因此,问题是: 是否存在可以无限期编译的C ++代码?

(Nested includes or nested templates are detectable so they should not count.) (嵌套的包含或嵌套模板是可检测到的,因此不应计数。)

Related question: Is there a C++ code that compiles with infinite memory? 相关问题: 是否有可以用无限内存编译的C ++代码? (I separated them since I expect different answer.) (我将它们分开,因为我希望得到不同的答案。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM