help-gplusplus
[Top][All Lists]
Advanced

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

Avoiding "spurious" compiler warning


From: Lionel B
Subject: Avoiding "spurious" compiler warning
Date: 3 Nov 2004 03:58:28 -0800
User-agent: G2/0.2

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?

Regards,
 
-- 
Lionel B



reply via email to

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