Topic: reuse of names in repetitive if-conditions
Author: kristov@arcor.de ("Christoph Schulz")
Date: Wed, 8 Oct 2003 19:06:42 +0000 (UTC) Raw View
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 ]
Author: johnchx2@yahoo.com (johnchx)
Date: Thu, 9 Oct 2003 03:39:24 +0000 (UTC) Raw View
kristov@arcor.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 ) ;
}
---
[ 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 ]
Author: kristov@arcor.de ("Christoph Schulz")
Date: Sat, 11 Oct 2003 01:44:29 +0000 (UTC) Raw View
Hello!
johnchx <johnchx2@yahoo.com> wrote:
> kristov@arcor.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 ]
Author: johnchx2@yahoo.com (johnchx)
Date: Mon, 13 Oct 2003 04:28:02 +0000 (UTC) Raw View
kristov@arcor.de ("Christoph Schulz") wrote
> 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.
> 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();
}
> 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 ]