How is the three-way comparison operator different from subtraction?

后端 未结 3 1025
忘了有多久
忘了有多久 2021-01-31 01:35

There\'s a new comparison operator <=> in C++20. However I think in most cases a simple subtraction works well:

int my_strcmp(const char *a, c         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-31 02:03

    Here are some cases that subtraction won't work for:

    1. unsigned types.
    2. Operands that cause integer overflow.
    3. User-defined types that don't define operator - (perhaps because it's not meaningful - one may define an order without defining a notion of distance).

    I suspect this list is non-exhaustive.

    Of course, one can come up with workarounds for at least #1 and #2. But the intent of operator <=> is to encapsulate that ugliness.

提交回复
热议问题