🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Brian M Kennedy Tue, 18 Jun 1991 21:04
sizeof('a')
#1336
Author: Brian M Kennedy
Date: Tue, 18 Jun 1991 21:04
32 lines
1629 bytes
>=daniel=>
>This means that the following program is legal in C++ and
>ANSI C, and has different meanings in the two languages.
>Am I correct in thinking that a design goal of Bjarne's was
>to avoid the possibility of such a program?
>Is that design goal sufficiently important to motivate x3j16
>to change the type of 'c' to int?
>[... program exercising sizeof('a') ...]

Yes and no.
Yes, 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.

=======================================
== Brian M. Kennedy <bmk@csc.ti.com> ==
== Computer Systems Laboratory ========
== Computer Science Center ============
== Texas Instruments ==================
Re: sizeof('a')
#1338
Author: 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
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