This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Can I avoid "shift count is negative" warning? (involves template parameter)
- From: me22 <me22 dot ca at gmail dot com>
- To: "John Love-Jensen" <eljay at adobe dot com>
- Cc: "Phil Endecott" <spam_from_gcc_help at chezphil dot org>, "MSX to GCC" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 3 May 2007 17:56:40 -0400
- Subject: Re: Can I avoid "shift count is negative" warning? (involves template parameter)
- References: <1178224961332@dmwebmail.belize.chezphil.org> <C25FB512.21858%eljay@adobe.com>
On 03/05/07, John Love-Jensen <eljay@adobe.com> 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?