对于如下代码段:(或检查 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/