【问题标题】:Warning for overflow in std::accumulatestd::accumulate 溢出警告
【发布时间】:2022-01-23 03:26:53
【问题描述】:

init arg 的类型与您正在累积的包含类型不匹配时,是否有任何方法可以获得 std::accumulate 警告?例如在这个例子中,当迭代一个 64 位整数的向量时,我们不应该累积一个 32 位的值。但是很容易只传递 0 而忘记传递 0LL。有没有办法得到这个警告? -Wall -Wextra -Wconversion 似乎没有帮助。我还尝试寻找可能有用的 clang tidy 检查,但也没有找到任何东西。

std::vector<long long> a = {10000000000, 10000000000};
cout << std::accumulate(a.begin(), a.end(), 0) << "\n"; // overflows
cout << std::accumulate(a.begin(), a.end(), 0LL) << "\n"; // prints 20000000000

【问题讨论】:

    标签: c++ warnings clang-tidy


    【解决方案1】:

    哦,我找到了正确的 clang-tidy 检查: misc-fold-init-type

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2019-09-23
      • 2021-03-03
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多