2 ms·
Even if so, it should have been a separate type. The "elegance" of this design can be complemented by having a better interface. My strawman example: /* Th
by lifthrasiir 10y ago
Even if so, it should have been a separate type. The "elegance" of this design can be complemented by having a better interface. My strawman example:
/* This is for making a type that can be compared but not calculated.
* Use it like `(const void *) _StrcmpResult[i]` for i = 0, 1 or 2.
* i = 0 indicates less, 1 indicates equal, and 2 indicates greater. */
extern const char _StrcmpResult[2];
const void *_Strcmp(const char *a, const char *b);
#define strcmp(a, op, b) (_Strcmp((a), (b)) op (const void*) &_StrcmpResult[1])
Now you are almost unable to incorrectly use this interface: `strcmp(a, <, b)` and `strcmp(a, !=, b)` etc. works, `strcmp(a, -, b)` does not compile (because we've casted to `const void*`), `strcmp(a, =, b)` also does not compile (because `_Strcmp()` is not a lvalue). Other incorrect uses will likely result in compile errors, though in possibly more obscure ways.