Topic: Casting of lvalues - need language lawyer
Author: peterk@borland.com (Peter Kukol)
Date: Sat, 26 Nov 1994 02:12:46 GMT Raw View
>void foo()
>{
> unsigned short bs;
> (int)bs = -1;
>};
Sadly, this used to be pretty common practice with DOS "C" compilers, and
both VC++ and (16-bit versions of) Borland C++ accept the above when it's
compiled as "C" code (16-bit versions of BC++ also accept it in C++, as
long as strict -A mode is not enabled). The 32-bit versions of BC++ always
reject the above code as an error, though, and to my knowledge Watcom has
always (correctly) rejected it. You've probably seen the reference to the
ARM where this is outlawed; for ANSI "C", check section 3.3.4 for the
same.
This construct is a frequent source of confusion, but hopefully it will
disappear as fewer and fewer implementations tolerate it.
Peter
Author: jxski@wiretap.spies.com (Xski)
Date: Wed, 23 Nov 1994 20:58:13 GMT Raw View
Given the following code snippet:
void foo()
{
unsigned short bs;
(int)bs = -1;
};
MSC and WatcomC both flag the assignment as an error. I *KNOW* its
crappy coding style, but why is it an error in C++? I've looked through
various and sundry C++ books (ARM, etc) but can find no reference to this
particular construct. If I change the cast to (int&) MSC goes with it
and the value is as expected.
FWIW, I've just started a new job and I'm trying to clean up the code
(as you can see, it needs it) and I would really like to give good reasons
for proposed changes in coding style other than "its just a Bad Thing".
Please respond via email
TIA
-jmr
Author: mfx@cs.tu-berlin.de (Markus Freericks)
Date: 24 Nov 1994 12:52:09 GMT Raw View
In article <CzqnL1.K11@wiretap.spies.com> jxski@wiretap.spies.com (Xski) writes:
> Given the following code snippet:
>
> void foo()
> {
> unsigned short bs;
> (int)bs = -1;
> };
>
> MSC and WatcomC both flag the assignment as an error. I *KNOW* its
> crappy coding style, but why is it an error in C++?
[..]
Look at the ARM, Section 5.4, p. 69. It says "The result of a cast to a
reference type is an lvalue; the results of other casts are not". and
gives the example
int i = 1;
(float) i = 1.2; // error
(float&)i = 1.2; // nonportable
Hope that helps,
Markus