public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/44021]  New: Templates with -Wtype-limits produces warnings.
@ 2010-05-07  7:14 crossroads0000 at googlemail dot com
  2010-05-07  8:49 ` [Bug c++/44021] " jwakely dot gcc at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: crossroads0000 at googlemail dot com @ 2010-05-07  7:14 UTC (permalink / raw)
  To: gcc-bugs

-Wtype-limits should not produce warnings in the following example:

template<class integer>
bool foo(integer x)
{
        if(x >= 0)
        {
                // Do something here if integer is positive.
                return false;
        }
        else if(x < 0) // Used else if() here instead of just else to get 2nd
warning.
        {
                // Do something else here if integer is negative.
                return true;
        }
}

int main()
{
        foo<unsigned int>(3);
        foo<int>(3);
        return 0;
}

The first line in the main() function, together with -Wtype-limits (or
-Wextra), produces these warnings:

        warning: comparison of unsigned expression >= 0 is always true
        warning: comparison of unsigned expression < 0 is always false

While these warnings makes sense for all other cases, they should not occur for
comparison of template types, since it does not easily allow for warning-free
templates which can handle unsigned and signed integers and have different
behavior for negative numbers.

Disabling the warning with -Wno-type-limits works as a temporary solution, but
this also makes finding potential bugs in non-template code more difficult.

Regards,
Jason.


-- 
           Summary: Templates with -Wtype-limits produces warnings.
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: crossroads0000 at googlemail dot com
 GCC build triplet: x86_64-linux-elf
  GCC host triplet: x86_64-linux-elf
GCC target triplet: x86_64-linux-elf


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/44021] Templates with -Wtype-limits produces warnings.
  2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
@ 2010-05-07  8:49 ` jwakely dot gcc at gmail dot com
  2010-05-07 11:22 ` crossroads0000 at googlemail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2010-05-07  8:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from jwakely dot gcc at gmail dot com  2010-05-07 08:49 -------
this seems like a good candidate for using template specialisation to alter the
behaviour

I think the warning is still useful for templates, since it warns you of a
potential mistake in your logic.  It doesn't make sense to have a template that
can handle unsigned integers with negative values so I want to be warned if I
write such code.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/44021] Templates with -Wtype-limits produces warnings.
  2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
  2010-05-07  8:49 ` [Bug c++/44021] " jwakely dot gcc at gmail dot com
@ 2010-05-07 11:22 ` crossroads0000 at googlemail dot com
  2010-05-07 12:10 ` redi at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: crossroads0000 at googlemail dot com @ 2010-05-07 11:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from crossroads0000 at googlemail dot com  2010-05-07 11:22 -------
Having to specialize for every unsigned or signed integer types makes no sense,
depends on the standard being used (e.g. if there is a long long type or not),
and whether or not the given implementation has added additional types (which
the standard allows). Also, it would completely defeat the purpose of having
templates in the first place. So template specialization is not really a good
solution.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/44021] Templates with -Wtype-limits produces warnings.
  2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
  2010-05-07  8:49 ` [Bug c++/44021] " jwakely dot gcc at gmail dot com
  2010-05-07 11:22 ` crossroads0000 at googlemail dot com
@ 2010-05-07 12:10 ` redi at gcc dot gnu dot org
  2010-05-07 12:20 ` redi at gcc dot gnu dot org
  2010-05-07 13:16 ` bangerth at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-05-07 12:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from redi at gcc dot gnu dot org  2010-05-07 12:09 -------
I didn't suggest specialising on every type, you could specialise on
numeric_limits<integer>::is_signed


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/44021] Templates with -Wtype-limits produces warnings.
  2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
                   ` (2 preceding siblings ...)
  2010-05-07 12:10 ` redi at gcc dot gnu dot org
@ 2010-05-07 12:20 ` redi at gcc dot gnu dot org
  2010-05-07 13:16 ` bangerth at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-05-07 12:20 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from redi at gcc dot gnu dot org  2010-05-07 12:20 -------
alternatively you could just use std::less<integer>()(x, 0) which avoids the
warning


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/44021] Templates with -Wtype-limits produces warnings.
  2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
                   ` (3 preceding siblings ...)
  2010-05-07 12:20 ` redi at gcc dot gnu dot org
@ 2010-05-07 13:16 ` bangerth at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: bangerth at gmail dot com @ 2010-05-07 13:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from bangerth at gmail dot com  2010-05-07 13:15 -------


*** This bug has been marked as a duplicate of 11856 ***


-- 

bangerth at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44021


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-05-07 13:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-07  7:14 [Bug c++/44021] New: Templates with -Wtype-limits produces warnings crossroads0000 at googlemail dot com
2010-05-07  8:49 ` [Bug c++/44021] " jwakely dot gcc at gmail dot com
2010-05-07 11:22 ` crossroads0000 at googlemail dot com
2010-05-07 12:10 ` redi at gcc dot gnu dot org
2010-05-07 12:20 ` redi at gcc dot gnu dot org
2010-05-07 13:16 ` bangerth at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).