Thread View: net.lang.c++
4 messages
4 total messages
Started by Bjarne Stroustrup
Sat, 06 Sep 1986 01:13
(or does it?)
Author: Bjarne Stroustrup
Date: Sat, 06 Sep 1986 01:13
Date: Sat, 06 Sep 1986 01:13
44 lines
1906 bytes
1906 bytes
> From: stephen@comp.lancs.ac.uk (Stephen J. Muir)
> Newsgroups: net.lang.c++
> Subject: C++ doesn't output unsigned correctly.
> Organization: Department of Computing at Lancaster University, UK.
> This program incorrectly prints minus one:
>
> # include <stream.h>
>
> main ()
> { unsigned x = -1;
> cout << x << "\n";
> }
>
> C++ version 1.1, BSD 4.2, DEC VAX-11/750.
``that is not a bug; it is a feature''. To get the desired answer 4294967295
you must use some kind of formatting function; for example:
cout << form("%u\n",x);
Maybe there ought to be a separate function ``unsig()'' like the ``oct()''
and ``hex()'' functions, but currently there is not.
In general: the stream library needs work - and will receive it.
The general problem of which this problem with unsigned is a symptom is that
C++'s resolution mechanism for overloaded functions cannot distinguish an
``unsigned'' from an ``int''. For example, if one could declare
overload f(unsigned), f(int);
which function would f(-1) call? which one would f(1) call? why?
I am not convinced that there are no answers, but currently the declaration
of f above is illegal.
There are other type differences that the overloading mechanism cannot distinguish:
char, short, int: there are no rvalues of type char or short
float, double: there are no rvalues of type float
(will be fixed when ANSI C gets accepted)
T, const T: can be a problem
T, T&:
oct, dec, hex, and unsignedness
Author: Jim Williams
Date: Mon, 08 Sep 1986 18:51
Date: Mon, 08 Sep 1986 18:51
39 lines
2060 bytes
2060 bytes
In article <6021@alice.uUCp> bs@alice.UucP (Bjarne Stroustrup) writes: >Maybe there ought to be a separate function ``unsig()'' like the ``oct()'' >and ``hex()'' functions, but currently there is not. I think that adding arguments to existing functions would be better than adding functions. i.e. hex(0xFFFFFFFF, 0, UNSIGNED) would produce "FFFFFFFF", but hex(0xFFFFFFFF, 0, SIGNED) would produce "-1". I prefer this solution to adding functions, since it can be general, and can be used with oct, and dec, and any new functions people think up. SIGNED and UNSIGNED can be implemented in several ways, and would be defined in stream.h. This solution also allows each function to choose what its default would be. I imagine dec() having a default of SIGNED, whereas hex(), and oct() would have UNSIGNED as default. By the way, the current inplementation of dec() will not print the most negative long integer correctly on a 2's complement machine. (The first thing the code does is to change a negative argument into a positive one, and set a sign flag. This is a big lose on a 2's complement machine!) Also, the second argument to str() does NOT truncate the string the way the other functions do. If the string is too long it just overflows, and makes it as long as it has to be (which is what an argument of 0 does). I definitely consider this a bug. Do you consider it feature? -- Jim \/\/illiams jim@mimsy.umd.edu umcp-cs!jim.UUCP
Re: (or does it?)
Author: Frank Adams
Date: Tue, 09 Sep 1986 23:54
Date: Tue, 09 Sep 1986 23:54
26 lines
1856 bytes
1856 bytes
In article <6021@alice.uUCp> bs@alice.UUCP writes: >The general problem of which this problem with unsigned is a symptom is that >C++'s resolution mechanism for overloaded functions cannot distinguish an >``unsigned'' from an ``int''. For example, if one could declare > > overload f(unsigned), f(int); > >which function would f(-1) call? which one would f(1) call? why? From K&R, pg. 180 (Stroustrup has identical wording on pg. 246): A decimal constant whose value exceeds the largest signed integer is taken to be "long"; an octal or hexadecimal constant which exceeds the largest unsigned integer is likewise taken to be "long"; otherwise integer constants are taken to be "int". Therefore, both "1" and "-1" are of type "int", and both "f(1)" and "f(-1)" should invoke "f(int)". If you want to invoke the other function using a constant argument, try e.g. "f((unsigned) 1)". There may be some point allowing a terminal "u" (or "U") to specify an unsigned constant, so that "1u" would be of type "unsigned int"; but as the example shows, this is not strictly necessary. Frank Adams ihnp4!philabs!pwa-b!mmintl!franka Multimate International 52 Oakland Ave North E. Hartford, CT 06108
Re: (or does it?)
Author: Guy Harris
Date: Fri, 12 Sep 1986 18:53
Date: Fri, 12 Sep 1986 18:53
19 lines
1323 bytes
1323 bytes
> There may be some point allowing a terminal "u" (or "U") to specify an
> unsigned constant, so that "1u" would be of type "unsigned int"...
There's definitely a point in allowing that - ANSI C compatibility. From
the May 1, 1986 draft, p. 23:
The type of an integer constant is the first of the
corresponding list in which its value can be represented.
Unsuffixed decimal: "int", "long int", "unsigned long int";
unsuffixed octal or hexadecimal: "int", "unsigned int",
"long int", "unsigned long int"; suffixed by the letter "u"
or "U": "unsigned int", "unsigned long int"; suffixed by the
letter "l" or "L": "long int", "unsigned long int";
suffixed by both the letters "u" or "U" and "l" or "L":
"unsigned long int".
--
Guy Harris
{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
guy@sun.com (or guy@sun.arpa)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads