c++ - 警告 : zero as null pointer constant while comparing iterators

标签 c++ iterator clang compiler-warnings clang-tidy

对于如下代码段:(或检查 https://godbolt.org/z/15bqMj )

std::vector<int> v{1,2,1,2,1,2};
for (auto it = v.cbegin(); (it = find_if(it, v.cend(), somePredicate)) < v.cend(); ++it) {
    std::cout << *it << std::endl;
}

我收到了来自编译器的警告(clang 11-std=c++20 -Wzero-as-null-pointer-constant):

warning: zero as null pointer constant [clang-diagnostic-zero-as-null-pointer-constant]
  for (auto it = v.cbegin(); (it = find_if(it, v.cend(), somePredicate)) < v.cend(); ++it) {
                                                                         ^
/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_iterator.h:1080:5: note: while rewriting comparison as call to 'operator<=>' declared here
    operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
    ^
note: this fix will not be applied because it overlaps with another fix
warning: use nullptr [hicpp-use-nullptr,modernize-use-nullptr]
  for (auto it = v.cbegin(); (it = find_if(it, v.cend(), somePredicate)) < v.cend(); ++it) {
                                                                         ^
note: this fix will not be applied because it overlaps with another fix
warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]
  for (auto it = v.cbegin(); (it = find_if(it, v.cend(), somePredicate)) < v.cend(); ++it) {
                                                                         ^
                                                                         nullptr
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_iterator.h:1080:5: note: while rewriting comparison as call to 'operator<=>' declared here
    operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
    ^

这是误报警告,还是我只是在代码中编写了一个错误?

最佳答案

I just wrote a bug in my code?

不,您的代码中没有错误。

Is this a false-positive warning

也许是的。对于隐式使用的标准库函数的实现来说,这可能是正确的,但如果是这种情况,则警告消息不完整,因为它无法显示使用 0 的代码。

这也应该是一个错误,因为标准库 header 不应显示警告。

关于c++ - 警告 : zero as null pointer constant while comparing iterators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64800254/

相关文章:

c# - 将openCV的vector<Point2f>转换为C#的Collections::Generic::List<Windows::Point>

c++11 - 为什么我需要两次将迭代器解引用到智能指针而不是使用 operator->()?

clang - 对符号 __cxa_free_exception@@CXXABI_1.3 的 undefined reference

java - 在索引处停止迭代器

c++ - 如何使用 macports 安装 clang 3.1? (OSX,雪豹)

c - LLVM 能代表什么 C 不能?

c++ - 对拍摄照片并将其保存到文件的 opencv c++ 程序进行单元测试

c++ - 通过索引标签描述 boost multi_index 迭代器类型

c++ - 为什么我的程序不输出 i?

c# - 产量返回-内存优化