[英]What does comparing the result of the three-way comparison operator with nullptr do?
Given the example from cppreference on <=>
, we can simplify the example code to:鉴于
<=>
上的 cppreference示例,我们可以将示例代码简化为:
struct person {
std::string name;
std::string surname;
auto operator <=> (const person& p) const {
if (const auto result = name <=> p.name; result != 0) {
return result;
} else {
return surname <=> p.surname;
}
}
};
But my IDE (CLion 2020.2), via clang-tidy, warns that result != 0
should actually be result != nullptr
.但是我的 IDE (CLion 2020.2) 通过 clang-tidy 警告
result != 0
实际上应该是result != nullptr
。 I didn't know that we can compare std::###_ordering
with nullptr
.我不知道我们可以将
std::###_ordering
与nullptr
进行比较。 Cpprefence also claims that we should rather compare it with literal 0
. Cpprefence 还声称我们应该将其与文字
0
进行比较。 There is nothing about nullptr
there.那里没有关于
nullptr
的内容。
This code:这段代码:
int main() {
person p1{"ccc", "vvv"};
person p2{"aaa", "zzz"};
std::cout << (p1 < p2);
}
compiles (GCC 10.1.0, Rev3, Built by MSYS2 project) and yields identical results to both the 0
and the nullptr
version.编译(GCC 10.1.0,Rev3,由 MSYS2 项目构建)并产生与
0
和nullptr
版本相同的结果。
However, my IDE also warns me that I should " Clang-Tidy: Use nullptr " with p1 < p2
.但是,我的 IDE 也警告我,我应该“ Clang-Tidy:使用 nullptr ”与
p1 < p2
。 By applying the "fix", the code changes to std::cout << (p1 nullptr p2);
通过应用“修复”,代码更改为
std::cout << (p1 nullptr p2);
which, well, doesn't compile.哪个,嗯,不编译。 It hints that it may be a bug in clang-tidy, but it doesn't explain why we can compare the orderings with
nullptr
.它暗示它可能是 clang-tidy 中的一个错误,但它没有解释为什么我们可以将排序与
nullptr
进行比较。 Why we can, why does it work and why would we want that?为什么我们可以,为什么它有效,为什么我们想要它?
But my IDE (CLion 2020.2), via clang-tidy, warns that
result != 0
should actually beresult != nullptr
.但是我的 IDE (CLion 2020.2) 通过 clang-tidy 警告
result != 0
实际上应该是result != nullptr
。
This is wrong.这是错误的。 It should be
result != 0
.它应该是
result != 0
。 The comparison categories are specified to only be comparable against the literal 0
.比较类别被指定为仅可与文字
0
进行比较。
The check likely comes from the fact that the only real way to implement comparing against the literal 0
in C++20 is to take an argument whose type is some unspecified pointer or function pointer to pointer to member - and clang-tidy likely flags any 0
that you provide as an argument to a pointer as something that should be nullptr
.检查可能来自这样一个事实,即在 C++20 中实现与文字
0
进行比较的唯一真正方法是采用类型为某个未指定指针或 function 指向成员指针的参数的参数 - 并且 clang-tidy 可能会标记任何0
作为指针的参数提供的应该是nullptr
的东西。 Most of the time, this is correct.大多数时候,这是正确的。 Just not specifically here.
只是这里不具体。 And indeed, if you write
result != nullptr
it probably will compile - but that's wrong and you should not do it.事实上,如果你写
result != nullptr
它可能会编译 - 但这是错误的,你不应该这样做。
This is a clang-tidy problem, it needs to understand that for comparison categories, it should not flag the use of literal 0 in this way.这是一个整洁的问题,它需要了解对于比较类别,它不应该以这种方式标记文字 0 的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.