The only reason I can imagine for this behaviour is that the compiler/standard writers did not want it. Maybe C just shouldn't include generics. Especially not as part of the macro layer.
The macros in tgmath and more recently stdbit show why those could be necessary. If you have a set of functions for addition with overflow detection, say add_overflow{i,l,ll}, and you have a pair of ptrdiff_t’s or int32_t’s or whatnot that you know are standard integer types, and you want to use the appropriate add_overflow* function, can you do it? With _Generic you can. Without it I think you’re stuck providing sep…
BOOL AddOverflowUnsigned(BYTE* a, BYTE* b, INT32 sizea, INT32 sizeb)
{
BOOL IsMsb = PlatformIsMSB();
BOOL IsLsb = PlatformIsLSB();
//log error
if(!IsMsb && !IsLsb)
return FALSE;
//Early out for overflow. Assuming
if(sizea == sizeb && PlatformIsMSB())
{
BOOL AMsb = (a >> ((sizea * PlatformByteSize()) - 1)) & 1;
BOOL BMsb = (b >> ((sizeb * PlatformByteSize()) - 1)) & 1;
if(AMSB && BMsb)
{
//We overflow, early out.
return FALSE;
}
else if(sizea == sizeb && PlatformIsLSB())
//Code here.
//We add using bitwise operators so we can do it on any size.
//SUM = A XOR B XOR CARRY, return in A
//Impliment algo here.
return TRUE;
}
int main()
{
UINT64 a = 69;
UINT64 b = 420;
if(sizeof(a) !
return AddOverflowUnsinged(&a, &b, sizeof(a), sizeof(b));
}
But they have this in the STD now and also all the secure coding libs have this in it's header as a basic function. There's also the easier implementation of just adding a check to see if it will overflow, rather than add in the function.You also only have a few you can hardcode if you don't do it generic, since all will go back to the primitives of uint8, uint16 ....