我传递一个带有初始化捕获循环计数器的lambda,如下所示:
#include <iostream>
auto sq(int c, int x) { return c * x * x; }
struct S {
template<class Fun>
void for_each(Fun fun) const {
for (auto i = 1; i < 4; ++i) {
fun(i);
}
}
};
int main()
{
S s;
auto sum = 0;
s.for_each([&, i = 2](auto c) mutable {
sum += sq(c, i++);
});
std::cout << sum; // 70 = 1 * 4 + 2 * 9 + 3 * 16
}
对于最高7.0svn的g++和最高3.9.1的clang++,这一切都是无警告编译的。但是,对于clang++ 5.0SVN,我得到
prog.cc:18:20:警告:此用法不需要捕获lambda捕获'i‘-Wunused-lambda-capture s.for_each(&, i = 2可变{
即使它仍然打印出正确的答案。
clang :为什么我从clang得到这个新的Wunused-lambda-capture
警告?
https://stackoverflow.com/questions/41960347
复制相似问题