如何使用太空飞船< =>具有StrCMP样式功能的操作员?
假设我有一个带有struct cat
的C库,并且一个函数比较(cat a,cat b)
,该根据以下规则返回整数: -
- 如果a< b然后返回-1,
- 如果a = b,则返回0
- ,如果a> b然后返回+1
我正在编写C ++包装器(Say Catxx
,ct
作为C结构成员),并希望使用新的C ++ 20 SpaceShip操作员。
bool operator == (catxx& a, catxx& b)
{
return !compare(a.ct, b.ct);
}
auto operator <=> (catxx& a, catxx& b)
{
int result = compare(a.ct, b.ct);
return /*what ?*/;
}
我该怎么做?我无法理解订购概念。
- 如果我必须使用自定义而不是
compare()
,该怎么办? - 运算符的返回类型到底是什么?
- fear__erding,部分订购等是什么意思?
Suppose I have a C library with a struct cat
, and a function compare(cat a, cat b)
which returns an integer according for following rules :-
- if a < b then returns -1
- if a = b then returns 0
- if a > b then returns +1
I am writing c++ wrapper (say catxx
, with ct
as C struct member) for this library and would like to use the new C++20 spaceship operator.
bool operator == (catxx& a, catxx& b)
{
return !compare(a.ct, b.ct);
}
auto operator <=> (catxx& a, catxx& b)
{
int result = compare(a.ct, b.ct);
return /*what ?*/;
}
How would I do this ? I am unable to understand the ordering concept.
- What if I had to use custom
if else
instead ofcompare()
? - What exactly is return type of operator<=> ?
- What do weak_ordering, partial ordering etc. mean ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 cppreference :
因此,您可以简单地做到这一点
,因为操作数是整体类型,操作员会产生
std :: strong_ordering
。From cppreference:
So you can just simply do
Since the operands are integral type, the operator yields a prvalue of type
std::strong_ordering
.