On 03/05/07, John Love-Jensen <eljay@xxxxxxxxx> wrote:
Hi Phil, You can avoid it by using specialization. For a 32-bit int, that would be 32 positive, and 32 negative specializations. Maybe a bit much, but it'd work. HTH, --Eljay
I think something like this should work, without needing so many specializations: template <int a, bool n = a<0> struct rshift_by; template <int a> struct rshift_by<a, true> { static int shift(int b) { return b << -a; } } template <int a> struct rshift_by<a, false> { static int shift(int b) { return b >> a; } } template <int a> int shift (int b) { return rshift_by<a>::shift(b); } int f() { return shift<1>(1); return shift<-1>(1); } It also avoids the if branch on the compile-time constant, though GCC is quite good at optimizing those. (IIRC some other compilers (MSVC) whine about it, though.) ~ Scott McMurray P.S. I love how the template syntax gives a<0>, don't you?