I feel the need to play devil's advocate here. Poor use of macros are dangerous.
#define repeat(x) for(unsigned repeat_var=0;repeat_var<x;++repeat_var)
This is admittedly an interesting one... although the use of 'repeat_var' makes it a little awkward. Even if you can remember that the loop counter is named repeat_var, you still have other problems. The biggest one I can think of would be inability to nest loops.
Something like this would be better, IMO:
#define repeat(i,x) for(unsigned i=0; i< x; ++i)
That way you can nest loops and give each loop it's own counter.
But is is really worth it? for loops aren't exactly difficult/time consuming to write.
*shrug*
#define BIT(x,y) (((x)>>(sizeof(x)-(y)))&1)
I think I see what this is trying to do, but it doesn't work right. I think you have your bit numberings backwards (bit 0 is the least significant bit, not the most significant), and sizeof() returns the size in bytes, not bits.
#define DEBUG cout<<"DEBUG"
I don't see what practical use this would have. However it would conflict nicely with the DEBUG symbol that's likely already defined by the compiler.
#define arraysize(array) (sizeof(array) / sizeof((array)[0]))
The templated solution is superior:
|
template <typename T,unsigned S> inline unsigned arraysize(const T (&v)[S]){ return S; }
|
Misusing the macro version (by giving it a vector, pointer, or dynamically allocated array) will compile OK but give you bad results/runtime errors. However the template version will give you a compiler error if you try to misuse it.
#define check_bit(var, pos), toggle_bit, etc
These would all be better as inline functions. Also your clear_bit is missing a ~ ;P