Even More Cool Integer Tricks

OK, so this is just utterly geeky, and would really only come in handy if you’re writing something like SafeInt –

How to tell if a numeric template type is a bool at compile time:

isBool = ((T)1 == (T)2)

if type T is a bool, then this is true, else it’s an int.

How to tell if a type is an int or a float:

isFloat = ((T)1.1 > (T)1)

This one’s a little more obvious:

isSigned = ((T)-1 < 0)

SafeInt 3 is coming soon – got it all working with the test rig last week, fixed a couple of bugs.

Speaking of testing, this is how to do things – don’t even think about testing this thing with code review – tried that, and even many educated eyes didn’t find all the problems. Speaking of many eyes, SafeInt 1.0.7 and 2.0 have been public for a long time, and there were a couple of minor issues, but the many eyes haven’t found anything. Good thing I have a really good tester. Anyway, the test harness is cool – I just plug in the new code to the test harness, it doesn’t fuzz, but it does try every corner case you could ever find, and then I just fix bugs until the test harness quits complaining. Keeps me from introducing regressions, too. Even if you don’t have team programming, writing a test harness to validate your code and throw some evil inputs at it while you’re at it will seriously improve quality. It’s why a lot of the time, I’ll get something working outside of the Office build environment, then once I’m fairly sure it works, THEN I port it over.

Comments (2)

  1. Ryan Russell says:

    The many eyes thing isn’t working out, then? If people aren’t interested in security review for its own sake, maybe some other kind of motivation will make people want to look for bugs? 😉

    [dcl] I don’t think many eyes works very well anyway – what does work well is hard work. You’re right about this one.

  2. Chris Becker says:

    If you want even finer granularity, you can specialize a templated a function using something like:

    template< typename T >

    bool IsFloat<T>( T f ) { return false; }


    bool IsFloat<float>{ return true; }


    Yes, but that would return false for a double – which may be what you want.