我正在撰寫一些將 lambda 函式傳遞給一組遞回函式的代碼。其中一些 lambda 函式嵌套在其他 lambda 函式中。我想我正在撰寫有效的代碼,但我遇到了fatal error C1060: compiler is out of heap space錯誤。
這是代碼的精簡版本
struct Null
{
template <typename SK>
static void match(SK sk)
{
}
};
template <typename T>
struct Repetition
{
template <typename SK>
static void match(SK sk)
{ // <--------------------------------- error message points to this line
T::match([]() { match([]() {}); });
}
};
int main()
{
using Test = Repetition<Null>;
Test::match([](){});
}
現在這個最小版本沒有多大意義,但它有相同的編譯器錯誤。我已經指出了上面錯誤的行。
我的問題是,這是編譯器錯誤/限制還是我的代碼在某種程度上無效?
編譯器是編譯 C 20 的 Visual Studio 2022。
謝謝
uj5u.com熱心網友回復:
每[expr.prim.lambda.closure]
lambda 運算式的型別(也是閉包物件的型別)是唯一的、未命名的非聯合型別別
要實體化Repetition::match,編譯器必須實體化Null::match,這需要Repetition::match...的實體化,依此類推。每次編譯器遞回時,[]() {}都被視為一種全新的型別,無限回圈。
要讓它停止遞回,請將您的呼叫替換為:
T::match(sk);
uj5u.com熱心網友回復:
雖然 VS2022 和 gcc 11 和 12 都崩潰了,但 clang 確實設法給出了一條錯誤訊息:
<source>:15:25: fatal error: recursive template instantiation exceeded maximum depth of 1024
T::match([]() { match([]() {}); });
^
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: (skipping 1015 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:22:11: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:22:17)>' requested here
Test::match([](){});
^
1 error generated.
ASM generation compiler returned: 1
<source>:15:25: fatal error: recursive template instantiation exceeded maximum depth of 1024
T::match([]() { match([]() {}); });
^
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: (skipping 1015 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:15:25: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:15:31)>' requested here
<source>:22:11: note: in instantiation of function template specialization 'Repetition<Null>::match<(lambda at <source>:22:17)>' requested here
Test::match([](){});
^
1 error generated.
Execution build compiler returned: 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488657.html
上一篇:為什么lambda函式的每個實體化都會產生唯一的型別?
下一篇:矩陣類突然結束的C 程式
