A compiler difference & the C++ standard

E

Eric

Came across a strange compiler difference today and wasn't sure which
compiler had the bug.

void AFunction( Rect &aRect )
{
if ( 1 ) {
Rect aRect = { aRect.top, aRect.left, aRect.bottom, aRect.right };
}
}

Now, granted, this is a very weird way to write things (I didn't write
code this way :), but Microsoft Visual C++, aRect.top apparently
refers
to the aRect parameter that was passed into the function.

X-Code (which uses gcc as it's compiler) has aRect.top referring to
the aRect declared within the scope of
the if statement.

So, what does the C++ standard say?
 
A

Anand Hariharan

Came across a strange compiler difference today and wasn't sure which
compiler had the bug.

void AFunction( Rect &aRect )
{
if ( 1 ) {
Rect aRect = { aRect.top, aRect.left, aRect.bottom, aRect.right };
}

}

Now, granted, this is a very weird way to write things (I didn't write
code this way :), but Microsoft Visual C++, aRect.top apparently
refers
to the aRect parameter that was passed into the function.

X-Code (which uses gcc as it's compiler) has aRect.top referring to
the aRect declared within the scope of
the if statement.

So, what does the C++ standard say?

gcc is right. IIRC, Stroustrup says it is "silly and perverse, but
not illegal" for an identifier to be used as its own initialiser.

- Anand
 
S

SasQ

Dnia Wed, 28 Mar 2007 14:33:16 -0700, Eric napisa³(a):
void AFunction( Rect &aRect )
{
if ( 1 ) {
Rect aRect = { aRect.top, aRect.left, aRect.bottom, aRect.right };
}
}
So, what does the C++ standard say?

Declared identifier name is known from the place his declarator ends
[before the '=' sign in the declaration, if any]. So, in the code:

Rect aRect = { aRect.top, aRect.left, aRect.bottom, aRect.right };
|
'-------------------------.
the second aRect is known from here --' and starts to hide the
first aRect name [being the parameter of AFunction].
The reason was that the following should be possible and valid:

int x = x;

thus it is in most cases stupid ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top