By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

A compiler difference & the C++ standard

P: n/a
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?

Mar 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mar 28, 4:33 pm, "Eric" <ericg...@gmail.comwrote:
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

Mar 28 '07 #2

P: n/a
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 ;)

--
SasQ
Mar 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.