我试图找出以下代码片段是好还是坏做法。这是关于应该由我的 API 解析的 html 查询字符串。使用递归来修剪任意数量的“?”非常方便脱离查询字符串。
但是,我想知道这是否可能由于无法控制的递归深度而导致堆栈溢出。我希望这种情况能够保证进行尾部优化,但我不确定。有这样的保证吗?
#include <string_view>
#include <cstdio>
static auto digest_query(std::string_view query) -> void
{
if (query.front() == '?') {
// printf("%.*s\n", (int)query.size(), query.data());
return digest_query(query.substr(1));
}
// Do other stuff...
}
int main()
{
digest_query("???????key=value");
}
最佳答案
Is there such guarantee?
不,没有。
关于c++ - 堆栈溢出还是尾递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75408283/