Today, I have discovered a very simple (but yet very efficient and useful) feature from C++20: the std::cmp_* functions. They allow you to compare 2 integers, which can have different signedness, without getting bitten by implicit conversions.
Let’s have a look at this piece of code:
#include <iostream>
int main()
{
int a = -6;
unsigned int b = 3;
if (a < b) {
std::cout << "OK: a is lower than b\n";
} else {
std::cout << "Oopsi!\n";
}
}
Yes, you’ve guessed right: it prints “Oopsi!”.