이론적으로이 무한 '에 대한 컴파일 것 '시간 템플릿 확장은 무한 재귀 때문에 그러나
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++ 컴파일러의 컴파일에 무한한 시간이 걸리는 소스를 만들 수 있습니다. 실제 컴파일러와 현대 컴파일러? 아마 불가능합니다. –
심도 한계로 인해 컴파일 시간이 유한하지만 tt는 우주 수명보다 오래 컴파일하는 프로그램을 만들기가 쉽습니다. – zch