Dear Experts,
template <int a>
int shift (int b)
{
return (a>0) ? b>>a : b<<-a;
}
int f()
{
return shift<1>(1);
return shift<-1>(1);
}
$ g++ -W -c shiftwarn.cc
shiftwarn.cc: In function â??int shift(int) [with int a = 1]â??:
shiftwarn.cc:11: instantiated from here
shiftwarn.cc:5: warning: left shift count is negative
shiftwarn.cc: In function â??int shift(int) [with int a = -0x00000000000000001]â??:
shiftwarn.cc:12: instantiated from here
shiftwarn.cc:5: warning: right shift count is negative
Of course, the shift count is only negative in the branch of the the ?:
that isn't used.
I get the same warning if I use if-then-else rather than ?:. If 'a' is
a regular function parameter rather than a template parameter, or if it
is a constant, I don't get any warnings.
Is there any way to avoid this? Do you consider it a bug?
This is g++ 4.1.2.
Many thanks for any suggestions.
Phil.
p.s. I've just noticed that it doesn't warn about having two return statements...