|  | default option at the beginning of switch |  | |
| | | Guest |  |
| Posted: Wed Aug 20, 2008 2:28 pm Post subject: default option at the beginning of switch |  |
How is this program supposed to behave? Is this even valid?
The default option is placed at the front of the switch statement, modifying the original value:
#include <iostream> using namespace std;
enum ENumber { eNum1 = 0, eNum2, eNum3 };
void TestSwitch(ENumber eNum) { switch(eNum) { default: eNum = static_cast<ENumber>(eNum3 - eNum); case eNum1: cout << eNum << " mapped to: " << eNum1 << endl; break; case eNum2: cout << eNum << " mapped to: " << eNum2 << endl; break; case eNum3: cout << eNum << " mapped to: " << eNum3 << endl; break; } }
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Bo Persson |  |
| Posted: Wed Aug 20, 2008 8:58 pm Post subject: Re: default option at the beginning of switch |  |
| |  | |
sat_andi@yahoo.co.uk wrote:
| Quote: | How is this program supposed to behave? Is this even valid?
|
I don't know how it is supposed to behave, but it is syntactically correct. The order of the labels are not important.
| Quote: | The default option is placed at the front of the switch statement, modifying the original value:
|
It can modify the value if it wants to, as the switch (case selection) has already executed at that point. It will definitely not re-select a case. However, as there is no break for the default case, it will fall through to case eNum1. Is that really intentional??
| Quote: | #include <iostream using namespace std;
enum ENumber { eNum1 = 0, eNum2, eNum3 };
void TestSwitch(ENumber eNum) { switch(eNum) { default: eNum = static_cast<ENumber>(eNum3 - eNum); case eNum1: cout << eNum << " mapped to: " << eNum1 << endl; break; case eNum2: cout << eNum << " mapped to: " << eNum2 << endl; break; case eNum3: cout << eNum << " mapped to: " << eNum3 << endl; break; } }
|
Bo Persson
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Hakusa@gmail.com |  |
| Posted: Thu Aug 21, 2008 1:40 am Post subject: Re: default option at the beginning of switch |  |
| |  | |
On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote:
| Quote: | How is this program supposed to behave? Is this even valid?
|
Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is not one of the answers, you fix it, but go to the first case listed. The GCC assembly code shows that a switch statement is implemented as a bunch of if and goto statements. The code has already passed the gotos by the time they ever get to default and will not go back to them.
Here's my suggestion, if you REALLY do want what you think you do.
void TestSwitch(ENumber eNum) { switch( eNum ) { case valid_ans: break; default: TestSwitch( /* make eNum valid * / ); } }
But, then again, my suggestion could cause an infinite loop if your solution for making eNum valid fails. Might want to make a testing variable that auto-returns if there were too many iterations.
| Quote: | void TestSwitch(ENumber eNum) { switch(eNum) { default: eNum = static_cast<ENumber>(eNum3 - eNum); .... } }
|
The big logical flaw here is what makes eNum invalid. If it's less than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE wrong! If it's greater than 2, then 2 - x will be negative! No matter what, this won't make eNum valid; rethink it.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Nick Hounsome |  |
| Posted: Fri Aug 22, 2008 12:58 pm Post subject: Re: default option at the beginning of switch |  |
| |  | |
On 21 Aug, 02:40, "Hak...@gmail.com" <Hak...@gmail.com> wrote:
| Quote: | On Aug 20, 9:28 am, sat_a...@yahoo.co.uk wrote:
How is this program supposed to behave? Is this even valid?
Oh, it's valid, but Bo Persson pointed out its fatal flaw. If eNum is not one of the answers, you fix it, but go to the first case listed. The GCC assembly code shows that a switch statement is implemented as a bunch of if and goto statements. The code has already passed the gotos by the time they ever get to default and will not go back to them.
Here's my suggestion, if you REALLY do want what you think you do.
void TestSwitch(ENumber eNum) { switch( eNum ) { case valid_ans: break; default: TestSwitch( /* make eNum valid * / ); }
}
But, then again, my suggestion could cause an infinite loop if your solution for making eNum valid fails. Might want to make a testing variable that auto-returns if there were too many iterations.
void TestSwitch(ENumber eNum) { switch(eNum) { default: eNum = static_cast<ENumber>(eNum3 - eNum); ... } }
The big logical flaw here is what makes eNum invalid. If it's less than zero, then 2 (eNum3) - neg will be greater than eNum3, thus MORE wrong! If it's greater than 2, then 2 - x will be negative! No matter what, this won't make eNum valid; rethink it.
|
{ Edits: quoted clc++m banner removed. Since the banner is automatically appended to every article there is no need to quote it. -mod }
My guess is that it is a poor attempt to convert negative values into Enums greater than the max one (Enum3). As stated above, this fails if eNum > eNum3 initially but even if that isn't a problem it would be more clearly written as eNum = static_cast<ENumber>( eNum < 0 ? (eNum3-eNum) : eNum ); // or expand to 'if' if you prefer
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|