简体   繁体   English

将三路比较运算符的结果与 nullptr 进行比较有什么作用?

[英]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::###_orderingnullptr进行比较。 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 项目构建)并产生与0nullptr版本相同的结果。


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 be result != 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.

相关问题 C++ 中的 &lt;=&gt;(“宇宙飞船”,三路比较)运算符是什么? - What is the <=> ("spaceship", three-way comparison) operator in C++? 三向比较运算符总是有效吗? - Is the three-way comparison operator always efficient? 比较运算符与三向运算符的嵌套生成? - Nested generation of comparison operator with three-way operator? 顺序推导不一致的三向比较算子 - Three-way comparison operator with inconsistent ordering deduction 三向比较运算符与减法有何不同? - How is the three-way comparison operator different from subtraction? 为什么要使用三向比较运算符 (&lt;=&gt;) 而不是双向比较运算符? 这有优势吗? - Why should I use the three-way comparison operator (<=>) instead of the two-way comparison operators? Does this have an advantage? 函数指针的三路比较失败 - Three-way comparison of pointer to functions fails 三向比较运算符成员与非成员实现 - Three-way comparison operator member vs non-member implementation 如何使用三路比较(spaceship op)实现不同类型之间的operator==? - How to use three-way comparison (spaceship op) to implement operator== between different types? 为什么默认三通运算符(spaceship &lt;=&gt;)生成相等运算符(==)而用户定义三通运算符不? - Why default three-way operator (spaceship <=>) generates equality operator (==) and user define three-way operator not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM