noooo! (switch statement)

Whaaat?!?

A switch has to be an int in c++??!

surely not!

I have got some c# code to convert with loads of switches. How can i work it without a string switch :O

Converting them all to "if"s will be a total mission and wont work anyway as i need to stop the logic on switch.
Last edited on
in C++ switch is not only for int but only for integral types...
If it is really important look here.
http://tangiblesoftwaresolutions.com/Product_Details/Instant_CPlusPlus_CSharp_Edition.htm

You can convert 1000 lines of a project with the demo version. Not much but if you have little projects you want converted that will probably do.

I'm not sure if this works but it looks pretty good.
Last edited on
Correct. switch works only for integral types. Not floats, doubles, or objects. Just integers.

Keep in mind that if has a nesting limit <100, and that each else if is another nesting level.
What I do when I need to fork based on, say, C strings and I don't know that the number of strings won't grow over time, I do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char *cases[]={
	"case 1",
	"case 2",
	"case 3",
	"case 4",
	//...
	0
};
int *which=-1;
for (int a=0;which<0 && cases[a];a++)
	//s is what would ideally be inside the parentheses in the switch statement
	if (!strcmp(s,cases[a]))
		which=a;
switch (which){
	case 0: //"case 1"
		//...
		break;
	case 1: //"case 2"
		//...
		break;
	case 2: //"case 3"
		//...
		break;
	case 3: //"case 4"
		//...
		break;
	//...
}
What? Why would the compiler enforce a maximum nesting level of if() statements or anything else for that matter, unless it was a hard limitation of the compiler?

I sadly worked on a project where a single function had 128 if statements and 64 elses.

IMHO I think the above code is less useful than just a huge block of if-elses. If anything (pun not intended), I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct MenuItem {
    MenuItem( const char* item, const boost::function<void(void)>& fn ) :
        item( item ), fn( fn ) {}

    std::string item;
    boost::function<void(void)> fn;
};

template< typename T, size_t N >
size_t ArraySize( T (const &)[N] )
{  return N; }

static const MenuItem menu[] = {
    MenuItem( "Item 1", HandleItem1 ), // etc
};

MenuItem* target = std::find_if( menu, menu + ArraySize( menu ),
    &_1->*&MenuItem::name == s );
if( target )
    target->fn();


Note: did not attempt to compile this, and I usually don't get the
boost::lambda syntax right the first time.
What? Why would the compiler enforce a maximum nesting level of if() statements or anything else for that matter, unless it was a hard limitation of the compiler?
Okay, I probably should have said that some compilers may have that limitation. I do know that VC++ has it, and the limit is well below 100 levels.

Something else I've done when I knew that the number of options would be somewhat large is this:
typedef std::map<const wchar_t *,ErrorCode(Interpreter::*)(Line &),wstrCmp> commandListType;
Last edited on
Ah, yes, good 'ol Micro$oft.

:)

I program exclusively in Linux/GCC so I'm not familiar with those compilers.


Topic archived. No new replies allowed.