出于某种原因,这个 lambda 语句不想编译:
bool DepthFirstSearch = [](Graph *g, bool *ch, stack<int> &S, int v, int w) -> bool
{
//Here is recursive DFS code
};
错误:不存在从“lambda[]bool()->bool”到 bool 的合适转换函数
。
我的问题是为什么?
最佳答案
lambda 的类型不是bool
。您可以使用 auto
来完成这项工作
auto DepthFirstSearch = [](Graph *g, bool *ch, stack<int> &S, int v, int w) -> bool { ... }
或使用std::function
明确指定返回类型,但那样会很麻烦。参见 http://en.cppreference.com/w/cpp/utility/functional/function .
通常只使用 auto 会更容易,否则每次更改 lambda 的参数时都必须更改变量的类型。
关于c++ - 递归 boolean lambda C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27025406/