I need to mask out some leading bits of an value. If the value is unsigned, I can assert (guarantee) that the some arbitrary number of leading bits are not set, that is the value is guaranteed to be limited.
If it is signed, I need to mask out the leading bits (turning the value into some nonportable heap of bits, yes, I am aware of that :-)). I would like to save the masking operation if the value is unsigned.
So I basically have
template<typename T, some more template parameters>
class {
unsigned transform(T value) {
...
if (isSigned(T)) {
value &= mask;
}
...
}
}
Is there an easy way to write an isSigned() which can be evaluated at compile time (to enable the optimizer to remove the unsigned dead code)?
Of course I could add another template parameter...