考虑这个例子:
#include <algorithm>
#include <iostream>
int main()
{
std::string str = "abcde4fghijk4l5mnopqrs6t8uvwxyz";
std::string str2;
std::remove_copy_if(str.begin(), str.end(),
std::back_inserter(str2),
[](char& c) {
if (std::isdigit(c))
return true; // <----- warning here
else
return false;
}
);
std::cout << str2 << '\n';
}
Run Code Online (Sandbox Code Playgroud)
使用GCC 4.6.1,这可以很好地编译并打印预期的输出(字母表)但是我得到一个警告说"只有在return语句是函数体中唯一的语句时才能推断出lambda返回类型".
现在,我知道如何摆脱警告(使用尾随返回类型或只是说return isdigit(c);),但我很好奇,因为编译器没有任何警告(或者它应该是):代码中可能出错的地方像这样?标准是否对此有所说明?