是否有可以无限期编译的 C++ 代码?
Is there a C++ code that compiles for infinite time?
我经常听说"C++ sources take a lot of time and memory to compile"。
我还听说 C++ 模板是图灵完备的,所以它可能会遇到 Halting problem。
我还构建了一个 C++ 项目,它需要 8 GiB 的内存和 2 小时的时间。
所以,问题是:是否有无限期编译的 C++ 代码?
(嵌套包含或嵌套模板是可检测的,因此不应计入。)
相关问题:Is there a C++ code that compiles with infinite memory?(我将它们分开,因为我希望得到不同的答案。)
理论上这会编译 'infinite' 时间,因为模板扩展是无限递归的:
template <size_t N>
struct eat
{
static constexpr size_t value = eat<N+1>::value;
};
然而,我使用过的所有编译器都可以抵御这种代码,并发出类似这样的错误:
/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;
...等等
编辑:
好吧,我觉得这个真的是无限的:
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;
}
我经常听说"C++ sources take a lot of time and memory to compile"。
我还听说 C++ 模板是图灵完备的,所以它可能会遇到 Halting problem。
我还构建了一个 C++ 项目,它需要 8 GiB 的内存和 2 小时的时间。
所以,问题是:是否有无限期编译的 C++ 代码?
(嵌套包含或嵌套模板是可检测的,因此不应计入。)
相关问题:Is there a C++ code that compiles with infinite memory?(我将它们分开,因为我希望得到不同的答案。)
理论上这会编译 'infinite' 时间,因为模板扩展是无限递归的:
template <size_t N>
struct eat
{
static constexpr size_t value = eat<N+1>::value;
};
然而,我使用过的所有编译器都可以抵御这种代码,并发出类似这样的错误:
/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;
...等等
编辑: 好吧,我觉得这个真的是无限的:
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;
}