Can I avoid "shift count is negative" warning? (involves template parameter)

jlh jlh@gmx.ch
Thu May 3 21:26:00 GMT 2007


Hello!

This might not apply to g++ 4.1.2, which is what you use, but I
don't have this version right here to test it.  It works fine for
gentoo's g++ 4.1.1 and produces no warnings:

template <int a>
int shift (int b)
{
	if (a > 0) {
		unsigned ua = a;
		return b >> ua;
	} else {
		unsigned ua = -a;
		return b << ua;
	}
}
template int shift<1>(int);
template int shift<-1>(int);

However, you will have to turn on optimization in order to get the
same efficient code than with your version (x86 here).

(I do get a warning for converting -1 to unsigned on g++ 3.4.6)

jlh



More information about the Gcc-help mailing list