为什么我会收到未使用的 lambda 捕获警告
Why am I getting an unused lambda capture warning?
我正在传递一个带有初始化捕获的循环计数器的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 {
即使它仍然打印出正确答案。现场示例
问:为什么我会收到来自 clang 的新Wunused-lambda-capture
警告?
您的代码有效。
Clang的警告是无稽之谈。
将此报告为错误。
相关文章:
- 警告处理为错误这里有什么问题
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 可组合的lambda/std::函数与std::可选
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- cppcheck在const std::string[]上引发警告
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- GCC对可能有效的代码抛出init list生存期警告
- 如何在BST的这个简单递归实现中消除警告
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 关于std::move的使用,是否有编译警告
- g++ 在某个类成员未初始化时不发出警告
- 如何将lambda作为模板类的成员函数参数
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- MSVC 19.22 在 Lambda 上发出 C4626 警告
- GCC 6.x警告有关Lambda可见性
- 当存在一行时,Cppcheck 不会输出警告:auto lambda = [](){};
- 为什么我会收到未使用的 lambda 捕获警告
- 为什么for_each+lambda触发器-Waggregate返回警告
- 编译器警告:无法推导lambda返回类型
- 没有clang警告或错误,如果c++ 11 lambda返回错误的类型