【发布时间】:2018-07-13 09:46:02
【问题描述】:
考虑以下代码sn-p:
#include <map>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>
using TargetsType = std::map<float, std::string>;
using TimesType = std::set<float>;
void foo (const TargetsType& targets,
const TimesType& times)
{
for (const auto& target : targets)
{
// fails to compile
TimesType::const_iterator iter1 = std::find_if(times.begin(),
times.end(),
[&(target.first)](float item)
{
return std::fabs(item - target.first) < 0.1f;
});
// compiles OK
TimesType::const_iterator iter2 = std::find_if(times.begin(),
times.end(),
[&](float item)
{
return std::fabs(item - target.first) < 0.1f;
});
}
}
iter1 的声明编译失败,报错如下:
error: expected ',' before '(' token
但是iter2的声明是可以的。
谁能解释为什么第一个声明不能编译?
【问题讨论】:
-
@Tyker 我先试过了。同样的错误。