Discussion:
typecast yields an rvalue?
(too old to reply)
Ray Mitchell
2009-10-28 15:54:01 UTC
Permalink
Hello,

Section 5.4.1 of ISO/IEC 14882:1998(E) states:

"The result of the expression (T) cast-expression is of type T. The result
is an lvalue of T is a reference type, otherwise the result is an rvalue."

Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't. Obviously
I forgot to read something else somewhere in the standard, but what?
Logically, of course, one could make the case that since the typecast is to
the same type as the variable actually is, the compiler simply ignores it,
but I'm trying to justify this behavior using the language standard.

void function()
{
int x;
(int)x = 5;
}

Thanks,
Ray
Ulrich Eckhardt
2009-10-28 16:27:44 UTC
Permalink
Post by Ray Mitchell
"The result of the expression (T) cast-expression is of type T. The
result is an lvalue of T is a reference type, otherwise the result is an
rvalue."
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes it does, just use the right compiler. ;)

If your compiler doesn't, that's a compiler defect. Note that MSC does have
a few extensions that allow e.g. binding a temporary to a non-const
reference, but that is not legal according to the C++ standard. In any
case, even MSC manages to emit a suitable warning when the type changes.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Igor Tandetnik
2009-10-28 16:55:56 UTC
Permalink
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8 (aka VS2005): I don't have anything more recent handy at the moment.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
David Lowndes
2009-10-28 20:41:29 UTC
Permalink
Post by Igor Tandetnik
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8 (aka VS2005): I don't have anything more recent handy at the moment.
FWIW, VC2010 beta 2 also compiles it without warning.

Dave
Stephan T. Lavavej [MSFT]
2009-10-28 21:34:20 UTC
Permalink
I can't repeat this often enough: Compile at W4! Compile at W4! Compile at
W4!

C:\Temp>type meow.cpp
void foo() {
int x = 0;
(int) x = 1729;
}

C:\Temp>cl /EHsc /nologo /W4 /c meow.cpp
meow.cpp
meow.cpp(3) : warning C4213: nonstandard extension used : cast on l-value

http://msdn.microsoft.com/en-us/library/yshyhfby.aspx

W4 warns about our evil extensions, so you can run away.

STL
Post by David Lowndes
Post by Igor Tandetnik
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8
(aka VS2005): I don't have anything more recent handy at the moment.
FWIW, VC2010 beta 2 also compiles it without warning.
Dave
David Lowndes
2009-10-28 22:11:41 UTC
Permalink
Post by Stephan T. Lavavej [MSFT]
I can't repeat this often enough: Compile at W4! Compile at W4! Compile at
W4!
I usually do, I usually do, I usually do

Honestly :)

Now...

Why isn't W4 the default for new projects the Wizard creates?

Do you want that 3x as well?

Dave
Scot Brennecke
2009-11-07 07:33:09 UTC
Permalink
Post by David Lowndes
Post by Stephan T. Lavavej [MSFT]
I can't repeat this often enough: Compile at W4! Compile at W4! Compile at
W4!
Why isn't W4 the default for new projects the Wizard creates?
Because Stephan doesn't get to dictate to the team who make the project
engine, and they know there are too many users who prefer to stay
ignorant and happy at W3 rather than suffer the tedium of making their
code better to avoid warnings at W4.
IOW, the choice is to make users happy rather than better. :)
Alex Blekhman
2009-11-08 08:56:36 UTC
Permalink
Post by Scot Brennecke
IOW, the choice is to make users happy rather than better. :)
This choice makes users happy only in the short run, until these
clueless users encounter nasty heisen-bug. Then users become very
unhappy.

Alex

Brian Muth
2009-11-02 20:21:31 UTC
Permalink
Great post. My chuckle of the day.

Brian
Stephan T. Lavavej [MSFT]
2009-10-28 21:34:20 UTC
Permalink
I can't repeat this often enough: Compile at W4! Compile at W4! Compile at
W4!

C:\Temp>type meow.cpp
void foo() {
int x = 0;
(int) x = 1729;
}

C:\Temp>cl /EHsc /nologo /W4 /c meow.cpp
meow.cpp
meow.cpp(3) : warning C4213: nonstandard extension used : cast on l-value

http://msdn.microsoft.com/en-us/library/yshyhfby.aspx

W4 warns about our evil extensions, so you can run away.

STL
Post by David Lowndes
Post by Igor Tandetnik
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8
(aka VS2005): I don't have anything more recent handy at the moment.
FWIW, VC2010 beta 2 also compiles it without warning.
Dave
David Lowndes
2009-10-28 20:41:29 UTC
Permalink
Post by Igor Tandetnik
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8 (aka VS2005): I don't have anything more recent handy at the moment.
FWIW, VC2010 beta 2 also compiles it without warning.

Dave
Igor Tandetnik
2009-10-28 16:55:56 UTC
Permalink
Post by Ray Mitchell
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes, the code shouldn't compile. Looks like a bug. Can reproduce in VC8 (aka VS2005): I don't have anything more recent handy at the moment.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Ulrich Eckhardt
2009-10-28 16:27:44 UTC
Permalink
Post by Ray Mitchell
"The result of the expression (T) cast-expression is of type T. The
result is an lvalue of T is a reference type, otherwise the result is an
rvalue."
Because of this it seems to me that the statement (int)x = 5; in the
following code should result in a compiler error, but it doesn't.
Yes it does, just use the right compiler. ;)

If your compiler doesn't, that's a compiler defect. Note that MSC does have
a few extensions that allow e.g. binding a temporary to a non-const
reference, but that is not legal according to the C++ standard. In any
case, even MSC manages to emit a suitable warning when the type changes.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Loading...