help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Avoiding "spurious" compiler warning


From: Guy Harrison
Subject: Re: Avoiding "spurious" compiler warning
Date: Wed, 03 Nov 2004 17:59:50 GMT
User-agent: KNode/0.7.7

Lionel B wrote:

> Greetings,
> 
> Using gcc (GCC) 3.3.3 (cygwin special) on W2k, Pentium 4:
> 
> // main.cpp
> 
> #include <limits>
> 
> template <class T> class A
> {
> public:
> void foo(T& x)
> {
> if (sizeof(T) > sizeof(unsigned long))
> x << std::numeric_limits<unsigned long>::digits;
> }
> };
> 
> int main()
> {
> typedef unsigned long ulong;
> 
> A<ulong> a;
> 
> ulong x;
> a.foo(x);
> 
> return 0;
> }
> 
> the above code generates the following warning:
> 
> g++ main.cpp
> main.cpp: In member function `void A<T>::foo(T&) [with T = ulong]':
> main.cpp:23:   instantiated from here
> main.cpp:11: warning: left shift count >= width of type
> 
> Tool completed successfully
> 
> Now this is technically correct (since the shift count here is equal to
> the width of x)... except one might have thought that g++ could have
> figured out that the "shift" statement will never be executed (and
> suppressed the warning), since the
> 
> sizeof(T) > sizeof(unsigned long)
> 
> condition - which depends on *compile time constants* - evaluates to
> false. The same code with the "typedef" line changed to:
> 
> typedef unsigned long long ulong;
> 
> compiles (as it should) without warnings.
> 
> Any work-arounds / ideas for suppressing the essentially spurious
> warning?

Hmm, I'm not sure I'd want to but fwiw...

template <typename T> inline void
asl(T& x, unsigned n)
{
 x <<= n;
}

...

if (sizeof(T) > sizeof(unsigned long))
 asl(x,std::numeric_limits<unsigned long>::digits)
;

...anything that dispenses with "x <<= literal" should suffice.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]