Date: Sat, 29 Mar 2025 19:02:22 +0000
It's horrible when you use a "std::uint16_t" only to find that it
promotes to a signed 32-Bit integer. Believe it or not, I recently
wrote the following code:
using std::uint_fast32_t;
// We can't use uint_fast32_t if it undergoes promotion,
// so instead use long unsigned (which might be 64-Bit)
typedef typename std::conditional<
std::is_same< uint_fast32_t, decltype(uint_fast32_t() +
uint_fast32_t()) >::value,
uint_fast32_t,
long unsigned >::type UIntType;
So how about if we could tell the compiler that we want to use a
specific integer type, and that we don't want it to promote. Perhaps
here's how we'd define such an integer:
short unsigned _NoPromo n = 0xFFFF;
And similarly, if we already have a variable, and we want to do some
calculations without it promoting at some point, then we make a
_NoPromo reference to it:
short unsigned original_variable = 0xFFFF;
short unsigned _NoPromo &safe_variable = original_variable;
safe_variable = ~safe_variable; // no promotion takes place here
promotes to a signed 32-Bit integer. Believe it or not, I recently
wrote the following code:
using std::uint_fast32_t;
// We can't use uint_fast32_t if it undergoes promotion,
// so instead use long unsigned (which might be 64-Bit)
typedef typename std::conditional<
std::is_same< uint_fast32_t, decltype(uint_fast32_t() +
uint_fast32_t()) >::value,
uint_fast32_t,
long unsigned >::type UIntType;
So how about if we could tell the compiler that we want to use a
specific integer type, and that we don't want it to promote. Perhaps
here's how we'd define such an integer:
short unsigned _NoPromo n = 0xFFFF;
And similarly, if we already have a variable, and we want to do some
calculations without it promoting at some point, then we make a
_NoPromo reference to it:
short unsigned original_variable = 0xFFFF;
short unsigned _NoPromo &safe_variable = original_variable;
safe_variable = ~safe_variable; // no promotion takes place here
Received on 2025-03-29 19:02:29