我正在传递一个带有init-captured循环计数器的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.0 SVN的g ++和高达3.9.1的clang ++,这些都可以免费编译。但是,对于clang ++ 5.0 SVN,我得到了
prog.cc:18:20: warning: lambda capture 'i' is not required to be captured for this use [-Wunused-lambda-capture] s.for_each([&, i = 2](auto c) mutable {
即使它仍打印出正确的答案。 Live Example
问题:为什么我会从clang收到这个新的Wunused-lambda-capture
警告?
答案 0 :(得分:15)
您的代码有效。
Clang的警告是无稽之谈。
将此报告为错误。