From -3554957757286836991
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,c4044a0e79c89b37
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-12-30 12:59:48 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!kibo.news.demon.net!mutlu.news.demon.net!demon!mail2news.demon.co.uk!devnull
From: allan_w@my-dejanews.com (Allan W)
Newsgroups: comp.std.c++
Subject: Re: comparing signed and unsigned integral values
Date: Mon, 30 Dec 2002 20:59:48 +0000 (UTC)
Organization: http://groups.google.com/
Lines: 59
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <7f2735a5.0212301238.c261aa9@posting.google.com>
References: <f6aM9.81569$TA6.1061548@news.chello.at>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: mail2news.demon.co.uk 1041281988 14546 10.0.0.1 (30 Dec 2002 20:59:48 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Mon, 30 Dec 2002 20:59:48 +0000 (UTC)
X-Received: from mulga.cs.mu.oz.au ([128.250.1.22])
	by news.demon.co.uk with esmtp (Exim 4.05)
	id 18T70k-0003mT-00
	for mail2news@news.news.demon.net; Mon, 30 Dec 2002 20:59:46 +0000
X-Received: from localhost (localhost [[UNIX: localhost]]) by mulga.cs.mu.OZ.AU
	id HAA17952; Tue, 31 Dec 2002 07:59:42 +1100 (EST)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Path: comp-std-cpp-robomod!not-for-mail
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Delivered-To: std-c++@ncar.ucar.edu
X-Newsgroups: comp.std.c++
X-NNTP-Posting-Date: 30 Dec 2002 20:38:36 GMT
X-Spam-Status: No, hits=-9.7 required=5.0
	tests=NOSPAM_INC,QUOTED_EMAIL_TEXT,REFERENCES,SPAM_PHRASE_02_03
	version=2.41
Xref: archiver1.google.com comp.std.c++:16505

georg@nospam.ucar.edu ("Georg D.") wrote
> The Standard says there are integral conversions (4.7) and what shall
> happen if the destination is signed or unsigned, but I could not find
> specification for the destination type of parameters of global comparison
> operators, e.g. "bool operator < (???, ???)".
> 
> e.g. "-1 < 2294967295"   -> false (32-bit int)
> with warning: comparing signed and unsigned values.

This is correct, but probably not what you intended (thus the warning).

-1 is a signed int. Presumably on a 32-bit computer, 2294967295 is an
unsigned long.

> Q1: I'd like to understand which conversions are "performed" prior to
> comparison.

See 5/9 (NOT 5.9):
    ... if either operand is unsigned long, the other shall be
        converted to unsigned long.

> I'd like to read your comments on this and/or get links to existing
> comments or discussions.

I've heard C++'s rules described as "preserved unsignedness." In other
words, if one of your variables is unsigned, your comparisons will be
unsigned.

Frankly, even in most assembly languages, I doubt you could do the
comparison you want without making two tests; one to compare the
signed quantity to zero, and the other to compare it as if it was
unsigned. So if you overload global or namespace functions to do the
comparison you want, you won't be losing anything...

   namespace georg {
      // It's sufficient to define these in terms of
      // signed and unsigned long, since all other
      // integral types can convert to one of these with no loss.
      // Otherwise we'd need 81 of these functions (for every
      // combination of signed/unsigned char/short/int/long,
      // plus "plain char").
      bool lessthan(  signed long lhs,   signed long rhs)
         { return          lhs<rhs; }
      bool lessthan(unsigned long lhs, unsigned long rhs)
         { return          lhs<rhs; }
      bool lessthan(unsigned long lhs,   signed long rhs)
         { return rhs>0 && lhs<rhs; }
      bool lessthan(  signed long lhs, unsigned long rhs)
         { return lhs<0 || lhs<rhs; }
   }

   int main() { std::cout << georg::lessthan(-1, 2294967295); } // True

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



