C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

reuse of names in repetitive if-conditions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Christoph Schulz
Guest





PostPosted: Wed Oct 08, 2003 7:06 pm    Post subject: reuse of names in repetitive if-conditions Reply with quote



Hello,

I have a question regarding declarations in if-statements.
Given the following code fragment:


if (int i = someFunc())
{
//...
}
else if (int j = someOtherFunc())
{
//...
}
else if (int i = yetSomeOtherFunc())
{
//...
}


Note that the third condition again uses "i" as the name of the
variable,
as the first condition does. I was very surprised that this code is
apperently legal.

As I understood the rules given in 6.4/1, this code can be rewritten as:

if (int i = someFunc())
{
//...
}
else
{
if (int j = someOtherFunc())
{
//...
}
else
{
if (int i = yetSomeOtherFunc())
{
//...
}
}
}

It seems that the second "i" is not directly "in the outermost block
of the substatement controlled by the first condition". So, I conclude,
the use of "i" in the third condition is correct.

Replacing "j" with "i" in the example above would be erroneous, given
the rules in 6.4/3.

Can anyone say
- whether my interpretation is correct and
- why this unintuitive approach has been chosen?

I'm not very comfortable with it because the rules basically say that
in such a form of a repetitive if/else-statement, you can reuse names
in condition every *second* if-part. That is a very bizarre rule and
hard
to explain.


Regards,
Christoph


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
johnchx
Guest





PostPosted: Thu Oct 09, 2003 3:39 am    Post subject: Re: reuse of names in repetitive if-conditions Reply with quote



[email]kristov (AT) arcor (DOT) de[/email] ("Christoph Schulz") wrote

Quote:
I'm not very comfortable with it because the rules basically say that
in such a form of a repetitive if/else-statement, you can reuse names
in condition every *second* if-part. That is a very bizarre rule and
hard to explain.

This was clarified in TC1:

http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#227

The DR makes it clear that the statement following the keyword "else"
constitutes its own local scope. Any names in declared in that scope
therefore hide identical names from the enclosing scope.

So the following is now legal:

int f();
void do_something(int);

int main() {
if ( int i = f() ) do_something( i ) ;
else if ( int i = f() ) do_something( i ) ;
else if ( int i = f() ) do_something( i ) ;
}

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Christoph Schulz
Guest





PostPosted: Sat Oct 11, 2003 1:44 am    Post subject: Re: reuse of names in repetitive if-conditions Reply with quote



Hello!

johnchx <johnchx2 (AT) yahoo (DOT) com> wrote:

Quote:
kristov (AT) arcor (DOT) de ("Christoph Schulz") wrote

I'm not very comfortable with it because the rules basically say
that in such a form of a repetitive if/else-statement, you can
reuse names in condition every *second* if-part. That is a very
bizarre rule and hard to explain.

This was clarified in TC1:

http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#227

The DR makes it clear that the statement following the keyword
"else" constitutes its own local scope. Any names in declared in
that scope therefore hide identical names from the enclosing
scope.

So the following is now legal:

int f();
void do_something(int);

int main() {
if ( int i = f() ) do_something( i ) ;
else if ( int i = f() ) do_something( i ) ;
else if ( int i = f() ) do_something( i ) ;
}


I don't quite understand this either. The example in 6.4/3

if (int x = f()) {
int x;
} else {
int x;
}

says that both definitions of "x" in the local scopes are
illegal. 6.4/3 is not affected by the DR you mentioned.
So how can it be that

if (int x = f()) {} else {int x;}

is disallowed, where

if (int x = f()) {} else if (int x = g()) {}

is allowed?

It would be OK if the declaration in the "if"-statement
would be seen as nested within the scope of the "if"-part,
i.e. if you would write each if-statement in braces. Take
the following example (from 6.4/3, slightly modified):

int x = 42;
if (int x = f())
{
if (int x = 23)
{
}
}
else
if (int x = g())
{
}

First, transformations according to 6.4/1 result in

int x = 42;
if (int x = f())
{
if (int x = 23)
{
}
}
else
{
if (int x = g())
{
}
}

Then, each if-statement is enclosed in braces, because
names within them are considered being part of a new local
scope. Then one can "pull" the declarations out of the
conditions:

int x = 42;
{ // start of scope of first "if"
int x;
if (x = f())
{
// defining another "x" is not allowed here (6.4/3)
{ // start of scope of nested "if"
int x;
if (x = 23)
{
// defining another "x" is not allowed here (6.4/3)
}
} // end of scope of nested "if"
}
else
{
// defining another "x" is not allowed here (6.4/3)
{ // start of scope of second "if"
int x;
if (x = g())
{
// defining another "x" is not allowed here (6.4/3)
}
} // end of scope of second "if"
// defining another "x" is not allowed here due (6.4/3)
}
} // end of scope of first "if"


Are this the desired semantics of "if" with regard to scopes?
I fail to find anything in 6.4 that would allow to see
if-statements behaving as if enclosed in braces. Perhaps the
first sentence of 6.4/3 is to express it:

A name introduced by a declaration in a condition [...] is
in scope from its point of declaration until the end of the
substatements controlled by the condition.

But I'm not sure.
Can anyone confirm my interpretation?


Best regards,
Christoph


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
johnchx
Guest





PostPosted: Mon Oct 13, 2003 4:28 am    Post subject: Re: reuse of names in repetitive if-conditions Reply with quote

[email]kristov (AT) arcor (DOT) de[/email] ("Christoph Schulz") wrote
Quote:
I don't quite understand this either. The example in 6.4/3

if (int x = f()) {
int x;
} else {
int x;
}

says that both definitions of "x" in the local scopes are
illegal. 6.4/3 is not affected by the DR you mentioned.

That's right, the DR doesn't repeal the "no redeclaration in an
outermost block" rule.

Quote:
So how can it be that

if (int x = f()) {} else {int x;}

is disallowed, where

if (int x = f()) {} else if (int x = g()) {}

is allowed?


The second if introduces yet another local scope (per 3.3.2/4), so the
second declaration of x is not in the outermost block. This is
essentially the same reasoning that makes it legal to write

int f();
void g();

int main() {
int x = 0;
if (int x = f() ) g();
}

Quote:
I fail to find anything in 6.4 that would allow to see
if-statements behaving as if enclosed in braces. Perhaps the
first sentence of 6.4/3 is to express it

I think 3.3.2 (Local Scope) para.4 is the reference, though the
wording could probably be clearer.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.