🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1338

Re: sizeof('a')

#1338
From: Morris M. Keesan
Date: Thu, 20 Jun 1991 19:49
46 lines
2674 bytes
In article <1991Jun18.210440.16325@csc.ti.com> bmk@m2.csc.ti.com (Brian M Kennedy) writes:

> . . . being consistent with C would have been a strong enough reason
> for Bjarne to have made the type of 'c' be int in the first place,
> EXCEPT that there is a stronger reason to have 'c' be char in C++:
> overloading.
>
> If I write:
>
>	 cout << 'A';
>	 cout << 65;
>
> I want overload resolution to choose operator<<(ostream&, char) for
> the first call and operator<<(ostream&, int) for the second.  If 'A'
> was type int then the first call would print the int value of A rather
> than the character A.  That would be very ugly.
>
> This is discussed by E&S (ARM) on p.9, 2.5.2, in the annotations.

The reason for 'a' to be a char is indeed for overload resolution to work,
as discussed in the ARM in 2.5.2, but the example given above is not a good
example.  In fact, in the implementations I'm familiar with (and I haven't
used 2.0, so I don't know if it's still true),

  cout << 'A'

prints the int value of A.  See "The C++ Programming Languange" by Stroustrup,
1987 edition.  On p. 9, 8.2.1, we see class ostream defined with
operator<<(char*) and operator<<(int), but no operator<<(char), and two pages
later, on page 229, we see
----------------------------------------------------------------------
   There are no character valued expressions in C++.  In particular, '\n' is
an integer (with the value 10 when the ASCII character set is used), so that

       cout << "x = " << x << '\n';

writes the number 10 and not the expected newline.
----------------------------------------------------------------------

The ARM does not address this issue, leaving ostreams, cout, and overloaded
<< operators as a library issue rather than a language issue.
What do 2.0 implementations do with this?  Is this an incompatibility that
will cause problems for C++ 1.2 programs passing "char"s to << when the
programs are compiled with C++ 2.0?
------------------------------------------------
Morris M. Keesan  	mmk@d62iwa.mitre.org
Technical Details
Message-ID: <MMK.91Jun20154932@d62iwa.mitre.org>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!snorkelwacker.mit.edu!linus!linus!mbunix!mmk
References: <1991Jun18.210440.16325@csc.ti.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!samsung!uunet!snorkelwacker.mit.edu!linus!linus!mbunix!mmk
From: mmk@d62iwa.mitre.org (Morris M. Keesan)
Newsgroups: comp.std.c++
Subject: Re: sizeof('a')
Message-ID: <MMK.91Jun20154932@d62iwa.mitre.org>
Date: 20 Jun 91 19:49:32 GMT
References: <1991Jun18.210440.16325@csc.ti.com>
Sender: news@linus.mitre.org (News Service)
Distribution: usa
Organization: The Mitre Corp., Bedford, MA.
Lines: 46
In-Reply-To: bmk@m2.csc.ti.com's message of 18 Jun 91 21:04:40 GMT
Nntp-Posting-Host: d62iwa.mitre.org