I have a function that we use to enforce type matches when setting into a buffer:
void SetUInt8(size_checker<uint8_t> val)
{
// make some static checks
}
usually, it is invoked like this:
// compile error, since you might mean to call the SetUInt() function
int myvar = 10;
SetUInt8(myvar);
// works fine
int8_t myvar = 1;
SetUInt8(myvar);
This call causes a warning because the 30
is interpreted as an int
:
SetUInt8(30);
But I really want this to be OK since 30 < 256. Ideally, I would like the invocation not to have to change, but I have come up with the following:
template<size_t T>
void SetUInt8()
{
ASSERT(T < 256);
// do other stuff
}
Which of course would be used like:
SetUInt8<30>();
Alternatively, I can cast when calling the function:
SetUInt8(uint8_t(30U));
Is there another way around the issue of 30 being converted to an int, or detecting its actual value if its a compile time constant?