Board index » cppbuilder » if-statement scope

if-statement scope


2005-09-13 01:36:30 AM
cppbuilder23
I the this (simplified) code which compiles with gcc but not with bcc32
because "value" is redefined in the "else-if" part.
Is there an easy way to make the if-scope working as in gcc? Renaming the
variable is a solution but one that means lot's of work for me because the
code (Qt4) contains many of these constructs.
int getX() { return 10; }
int main()
{
int x = getX();
if (int value = 10 - x) { // Earlier declaration of 'value'
cout << value << endl;
}
else if (double value = 11.2 - x) { // Multiple declaration for 'value'
cout << value << endl;
}
return 0;
}
--
Regards,
Andreas Hausladen
(andy.jgknet.de/blog)
 
 

Re:if-statement scope

"Andreas Hausladen" < XXXX@XXXXX.COM >wrote in message
Quote
int getX() { return 10; }

int main()
{
int x = getX();
if (int value = 10 - x) { // Earlier declaration of 'value'
cout << value << endl;
}
else if (double value = 11.2 - x) { // Multiple declaration for
'value'
cout << value << endl;
}
return 0;
}
Compiles with Comeaux's on line compiler and with MSVC7.1.
Fails with BCB6 update 4. Seems like a bug.
 

Re:if-statement scope

"Duane Hebert" < XXXX@XXXXX.COM >wrote:
Quote
Compiles with Comeaux's on line compiler and with MSVC7.1.
Fails with BCB6 update 4. Seems like a bug.
Yep - the if() statement opens a new scope - and therefore the previous
one may be overridden.
One possibility may be to change the code to explicitly handle the
scope:
if (int value = 10 - x)
{
cout << value << endl;
}
else
{
if (double value = 11.2 - x)
{
cout << value << endl;
}
}
which should semantically be the same, and may not (I have no compiler
to hand) confuse the compiler.
Alan Bellingham
--
Me <url:mailto: XXXX@XXXXX.COM ><url:www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:accu.org/>
The 2004 Discworld Convention <url:dwcon.org/>
 

{smallsort}

Re:if-statement scope

Alan Bellingham wrote:
Quote
which should semantically be the same, and may not (I have no compiler
to hand) confuse the compiler.
I tried this before but bcb does not like it. Even if I move it into a
"else do { if(..) ..} while(0)" scope.
--
Regards,
Andreas Hausladen
(andy.jgknet.de/blog)
 

Re:if-statement scope

Andreas Hausladen wrote:
Quote
Alan Bellingham wrote:


>which should semantically be the same, and may not (I have no compiler
>to hand) confuse the compiler.


I tried this before but bcb does not like it. Even if I move it into a
"else do { if(..) ..} while(0)" scope.

Won't the "else if" always fail? Is this not applicable to this thread?
int x = getX();
if (int value = 10 - x) { // Earlier declaration of 'value'
cout << value << endl;
}
else if (double value = 11.2 - x) { // Multiple declaration for 'value'
cout << value << endl;
}
Nate
 

Re:if-statement scope

Nate Lockwood < XXXX@XXXXX.COM >wrote:
Quote
Won't the "else if" always fail? Is this not applicable to this thread?
No, because there is no integer value x for which (11.2 - x) evaluates
to zero. On the other hand, when getX returns 10 (which it always
happens to do in the example), then (10 - x) _does_ evaluate to zero, so
the else branch is always called.
Quote
int x = getX();
if (int value = 10 - x) { // Earlier declaration of 'value'
cout << value << endl;
}
else if (double value = 11.2 - x) { // Multiple declaration for 'value'
cout << value << endl;
}
All of which is irrelevant, since the noted problem is that it fails to
compile.
Alan Bellingham
--
Me <url:mailto: XXXX@XXXXX.COM ><url:www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:accu.org/>
The 2004 Discworld Convention <url:dwcon.org/>
 

Re:if-statement scope

"Andreas Hausladen" < XXXX@XXXXX.COM >wrote:
Quote
Alan Bellingham wrote:

>which should semantically be the same, and may not (I have no compiler
>to hand) confuse the compiler.

I tried this before but bcb does not like it. Even if I move it into a
"else do { if(..) ..} while(0)" scope.
Erk!
OK, the compiler is definitely exhibiting a bug. Short of naming the
variable differently, I have no suggestions.
Alan Bellingham
--
Me <url:mailto: XXXX@XXXXX.COM ><url:www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:accu.org/>
The 2004 Discworld Convention <url:dwcon.org/>
 

Re:if-statement scope

Nate Lockwood wrote:
Quote
Won't the "else if" always fail? Is this not applicable to this thread?
I thought posting 33.5 MB of source code would be a little bit over the
top. So I decided to make a minimalitic application.
--
Regards,
Andreas Hausladen
(andy.jgknet.de/blog)
 

Re:if-statement scope

Alan Bellingham wrote:
Quote
OK, the compiler is definitely exhibiting a bug. Short of naming the
variable differently, I have no suggestions.
And that was what I wanted to avoid. Because with renaming the variables
my patch against Qt4 will become larger and even worse, more problematic
for newer Qt 4 releases.
--
Regards,
Andreas Hausladen
(andy.jgknet.de/blog)
 

Re:if-statement scope

This seems to be a bug. I've reported it to Borland.
.a
 

Re:if-statement scope

Alan Bellingham wrote:
Quote
Nate Lockwood < XXXX@XXXXX.COM >wrote:

>Won't the "else if" always fail? Is this not applicable to this
>thread?

No, because there is no integer value x for which (11.2 - x) evaluates
to zero. On the other hand, when getX returns 10 (which it always
happens to do in the example), then (10 - x) does evaluate to zero, so
the else branch is always called.

>int x = getX();
>if (int value = 10 - x) { // Earlier declaration of 'value'
>cout << value << endl;
>}
>else if (double value = 11.2 - x) { // Multiple declaration for
>'value' cout << value << endl;
>}

All of which is irrelevant, since the noted problem is that it fails
to compile.
Alan Bellingham
A workaround I suggest is;
int x = getX();
if (10-x)
{
int value = 10-x;
cout << value << endl;
}
else if (11.2 - x)
{
double value = 11.2-x;
cout << value << endl;
}
 

Re:if-statement scope

Rob wrote:
Quote
A workaround I suggest is;
If it would be that simple code but it isn't
Here a real example and that is only one (small) example of hundreds:
if (QLayoutWidget *widget = qobject_cast<QLayoutWidget*>(object)) {
return new QDesignerLayoutDecoration(widget, parent);
} else if (QWidget *widget = qobject_cast<QWidget*>(object)) {
if (FormWindow *fw = FormWindow::findFormWindow(widget)) {
QDesignerMetaDataBaseItemInterface *item =
fw->core()->metaDataBase()->item(widget->layout());
return item ? new QDesignerLayoutDecoration(fw, widget,
parent) : 0;
}
}
--
Regards,
Andreas Hausladen
(andy.jgknet.de/blog)
 

Re:if-statement scope

"vavan" < XXXX@XXXXX.COM >wrote in message
Quote
On Tue, 13 Sep 2005 10:55:32 +0200, "Andreas Hausladen"
< XXXX@XXXXX.COM >wrote:

>If it would be that simple code but it isn't

>return item ? new QDesignerLayoutDecoration(fw, widget,
>parent) : 0;

FYI another problem you may confront is that it isn't recommended to
use ?: operator with bcb since it sometimes might lead to memory leaks
(another codegen bug) ...
I thought this was only wrt VCL and especially AnsiString?
 

Re:if-statement scope

"vavan" < XXXX@XXXXX.COM >wrote in message
Quote
On Tue, 13 Sep 2005 09:17:32 -0400, "Duane Hebert" < XXXX@XXXXX.COM >
wrote:

>I thought this was only wrt VCL and especially AnsiString?

unfortunately no. see for example qc#3083
Yikes. Fortunately, I don't like the ? syntax much.