Bug 45553 - Warning Suppression: C++ Templates, Unsigned, and "comparison of unsigned expression < 0 is always false"
Summary: Warning Suppression: C++ Templates, Unsigned, and "comparison of unsigned exp...
Status: RESOLVED DUPLICATE of bug 11856
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-06 05:21 UTC by Jeffrey Walton
Modified: 2010-09-06 06:48 UTC (History)
9 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jeffrey Walton 2010-09-06 05:21:59 UTC
Hi All,

A way to suppress the warning generated from the following would be a welcome addition. A solution was proposed in [1], but I'd like a cleaner approach. I believe suppressing the warning for a template parameter is the cleanest way to dispose of the warning.

template< class T >
SomeClass<T>::SomeFunc(const T& t)
{
   // warning: comparison of unsigned expression < 0 is always false
   if(t < 0)
   {
       ...
   }
}

[1] http://gcc.gnu.org/ml/gcc-help/2010-08/msg00284.html
Comment 1 Andrew Pinski 2010-09-06 05:24:49 UTC

*** This bug has been marked as a duplicate of 11856 ***
Comment 2 Jeffrey Walton 2010-09-06 05:35:49 UTC
(In reply to comment #1)
> 
> *** This bug has been marked as a duplicate of 11856 ***
> 
Thanks Andrew. For the record, I don't really consider it a bug - its closer to undesired behavior when working with generics/templates and the way the parts mesh.
Comment 3 Andrew Pinski 2010-09-06 05:39:07 UTC
It is still a dup of bug 11856.  Note the use of bug here is really dealing with how do you describe all issues (enhancements or otherwise).  The use is not saying it is a "software bug" in the normal sense.

*** This bug has been marked as a duplicate of 11856 ***
Comment 4 Jakub Jelinek 2010-09-06 06:33:51 UTC
In G++ 4.6+ you can do:
int i;
unsigned j;

template <class T>
void
bar (const T& t)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
  if (t < 0)
#pragma GCC diagnostic pop
    {
      i++;
    }
}

void foo (void)
{
  bar (i);
  bar (j);
}
Comment 5 Jeffrey Walton 2010-09-06 06:45:02 UTC
(In reply to comment #4)
> In G++ 4.6+ you can do:
> int i;
> unsigned j;
> 
> template <class T>
> void
> bar (const T& t)
> {
> #pragma GCC diagnostic push
> #pragma GCC diagnostic ignored "-Wtype-limits"
>   if (t < 0)
> #pragma GCC diagnostic pop
>     {
>       i++;
>     }
> }
> 
> void foo (void)
> {
>   bar (i);
>   bar (j);
> }
> 
Thanks Jakub.

All those #pragmas look like MS code. I thought Stallman hated those things (I seem to recall #pragma once being the source of a lot of in-house bickering). Did he get over ruled by committee?
Comment 6 Andrew Pinski 2010-09-06 06:48:38 UTC
>I thought Stallman hated those things

The reason why Stallman hated them is that they did not work with macros and that changed with C99 adding support of  _Pragma which can be used in macros now.  So his argument against Pragma went away when that come in.