[comp.std.c++] Non-Portable pointer assignment?

daniel@cis.ucsc.edu (Daniel Edelson) (06/18/91)

.Article: 2813 of comp.std.c
.From: hansen@pegasus.att.com (Tony L. Hansen)
.Subject: Re: Non-Portable pointer assignment? 
.Date: 18 Jun 91 15:04:10 GMT
.
.< From: mcdaniel@adi.com (Tim McDaniel)
.< I would have been quite content to have seen the types and sizes of
.< 'xy' and 'x' differ.
.
.C++ has taken this tack: the type of 'x' is char while the type of 'xy' is
.int. It gives very nice properties when dealing with function and operator
.overloading.
.
.					Tony Hansen
.			    hansen@pegasus.att.com, tony@attmail.com
.				att!pegasus!hansen, attmail!tony


Thank you for pointing this out. 
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?

#include <stdio.h>

int main(void)
{
	printf("%d\n", sizeof('a'));
}


---
Daniel Edelson                 |  ``Recycle your garbage. Please don't
daniel@cis.ucsc.edu, or        |    make me come and copy after you.'' 
uunet!ucscc!terra!daniel       |

jbuck@forney.berkeley.edu (Joe Buck) (06/19/91)

Tony Hansen writes:
|> .C++ has taken this tack: the type of 'x' is char while the type of 'xy' is
|> .int. It gives very nice properties when dealing with function and operator
|> .overloading.

In article <17163@darkstar.ucsc.edu>, daniel@cis.ucsc.edu (Daniel Edelson) writes:
|> Thank you for pointing this out. 
|> 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? 

No, you aren't.  There are a number of deliberate differences.
The win from being able to say

	int foo;
	cout << "The answer is " << foo << '\n';

far exceeds the rare cases where a different answer is obtained
in C and C++.  You can still pass 'a' to a routine that expects
an int and have the right thing happen.

|> int main(void)
|> {
|> 	printf("%d\n", sizeof('a'));
|> }

But can you show me a real production program that produces
different answers?  People very rarely write sizeof('a').


--
Joe Buck
jbuck@galileo.berkeley.edu	 {uunet,ucbvax}!galileo.berkeley.edu!jbuck