c++ - Specification of lambda's return value doesn't work
问题描述
I've got a problem.
The funciton "__sub
" parses a string like "1x + (5y - 2)"
. Each time it sees "(
", it calls itself to parse exactly what is in parenthesis.
Here is some pseudocode, illustrating the problem:
auto __sub = [&needed_fn](const char *& iter, char end_at) -> int {
for (; *iter != end_at; iter++) {
if () {
int number = needed_fn(iter);
} else if (*iter == '(') {
int sub_result = __sub(iter, ')');
}
}
return 0; // temporarily, as for debugging purposes only needed
};
But this doesn't work. At first there was no specification of (-> int
).
And it doesn't work in both cases with or without that specification of the return value.
It says:
a.cpp: In lambda function:
a.cpp:97:22: error: use of ‘__sub’ before deduction of ‘auto’
int sub_result = __sub(it, ')');
解决方案
Suggestion: define __sub
as a std::function<int(const char *, char)>
std::function<int(const char * &, char)> __sub;
__sub = [&needed_fn](const char *& iter, char end_at) -> int {
for (; *iter != end_at; iter++) {
if ( /* ??? */ ) {
int number = needed_fn(iter);
} else if (*iter == '(') {
int sub_result = __sub(iter, ')');
}
return 0;
};
otherwise the compiler can't deduce (auto
) the type of __sub()
using the same __sub()
inside the body of __sub()
.
推荐阅读
- java - 查找数组中每个大小为 k 的窗口的模式
- php - 在 OS Monterey 中使用 brew 安装 PHP 时出错
- r - Save table in viewer as jpg or png in R
- php - SQLSTATE [42S22]:找不到列:在 laravel livewire 项目中
- mysql - 如何检查用户是否已经存在 facebook 身份验证?
- java - 无法使用 selenium webdriver 和 Java 单击登录按钮
- python - 如何根据python中的特定属性对列表中的子列表进行排序
- google-docs - 如何在利用操作转换的协作编辑软件中解决冲突
- go-packages - 更新 Go 包参考 - google.golang.org/protobuf
- python - 无法删除列表中的项目