什么是叮当检查的垃圾值

What's a garbage value for clang-check

本文关键字:检查 叮当 什么      更新时间:2023-10-16

我收到以下警告:

test.cpp:14:25: warning: The right operand of '/' is a garbage value
    return (std::abs(a) / size) > 10;
                        ^ ~~~~

对于这段代码:

#include <algorithm>
#include <complex>
#include <vector>
#include <iostream>
using namespace std;
double
pitchDetect(const std::vector<std::complex<double>> &dft,
                              unsigned int samplingRate) noexcept {
  if (dft.empty())
    return 0.0;
  auto it = find_if(begin(dft), end(dft),
                    [size = dft.size()](const std::complex<double> &a) {
    return (std::abs(a) / size) > 10;
  });
  return 0.0;
}

我不明白问题出在哪里!

这看起来像是在trunk:中修复的bug 22833

给lambda捕获参数一个显式值(C++14中的新特性)会导致分析器认为该值是未定义的。

作为一种变通方法,您可以尝试将init捕获提升到lambda:之外

  auto const size = dft.size();
  auto it = find_if(begin(dft), end(dft),
                    [size](const std::complex<double> &a) {
    return (std::abs(a) / size) > 10;
  });