#### Archived

This topic is now archived and is closed to further replies.

# switch on enumeration should be warning?

This topic is 5504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, In Stroustrup's book, (page 77), he says that if you switch on an enumeration, then the compiler can issue a warning if for example you miss one of the values. If I understand that right, then in the following code,
enum CONSTANTS {FRONT, BACK, ON};
CONSTANTS side = pla.vertSide(testV1);
switch(side) {
case FRONT: cout<<"front"<<endl; break;
case BACK: cout<<"back"<<endl; break;
}

the compiler *should* issue a warning that I forgot to handle the ON value right? Acutally, i know compilers dont *have* to give a warning if they dont want to, but seeing as how im using msvc++ 6.0, and its pretty snazzy, I thought that it might. Am I doing something wrong, or does msvc++ just not warn about this? thanks! [edited by - AndreTheGiant on July 25, 2003 11:31:49 PM]

##### Share on other sites
MSVC 6.0 doesn''t do a lot of things right.

##### Share on other sites
Such a warning would be annoying. I can think of a half-dozen scenarios in which not all enumeration values would be meaningful within a switch statement.

Just get in the habit of using assertions; it''s more robust anyway (since you might have to handle an out-of-range enumeration, which the compiler couldn''t flag):
switch(xxx){case ABC:   // blah   break;case DEF:   // blah   break;default:   ASSERT(false);}

How appropriate. You fight like a cow.

##### Share on other sites
I''ve never used MSVC++, but I know that in gcc some warnings have to be turned on and some warnings only work when optimization is turned on as well. Both might be things to look in to. Another thing is, if you have a default: case in your switch, then it shouldn''t be issuing a warning because you are covering all cases.

##### Share on other sites
You might have to turn on all warnings: -Wall in gcc(I think) and an option in project settings in msvc++.

##### Share on other sites
Try going to warning level 4 and see what it says.

Warning: Warning Level 4 gives you a lot of complete bullshit.

##### Share on other sites
quote:
Original post by Promit
Warning: Warning Level 4 gives you a lot of complete bullshit.

I disagree with that statement. If you always write code to cope with W4 you will avoid many bugs.

I''ve worked as a contractor helping to fix systems that don''t work well (performance problems, memory/resource leaks, crashes etc.). I''ve seen the most stupid bugs that easily could have been avoided if people only did 2 things:
- Build their code with W4 and "break on errors"
- Never ever ignore return values (from CloseHandle etc.)

With VC6 and earlier it''s been tough to do this as the windows headers and STL haven''t built clean on W4. With VS.Net they do, which is a really good thing. Treat /W4 and /WX as your friends as they will help you to find bugs.

If you know a certain warning is harmless (such as a for loop where you compare some unsigned value with the signed "int i" fix the problem instead of having a warning around. Change "i" to size_t (or whatever unsigned type you''re comparing with.
In some cases you can choose to #pragma warnings away if needed and you know it''s ok to do so. Just don''t ignore the warnings, they are there for a reason.

##### Share on other sites
quote:
Original post by Promit
Try going to warning level 4 and see what it says.

Warning: Warning Level 4 gives you a lot of complete bullshit.

You can use W4 and turn off the useless warnings with
#pragma warning ( disable: nnnn )

##### Share on other sites
Speaking of warning level 4...

Does anyone know of a way to get only code we write to use warning level 4 and the other headers (from libs, etc.) to use W3 or something?

I really could give a rat’s backside less about *some other person’s* flaky code if I can’t change it! (once I know about it that is...)

Tanks
Feral

##### Share on other sites
From visual studio, open project settings, select all configurations and change to W4. Then select only your precompiled file (stdafx.cpp) and set that to W4.
You could also use #pragma warning(push, 3) first in stdafx and #pragma warning(pop) in the end.

Make sure to include all external header files from stdafx.h. Your own cpp files should first include stdafx.h, then your header files needed. Never include external headers from any other place than stdafx.h.

VC6 isn''t too god in handling this, VS.Net is much better.

Your header files should in my opinion not include any other files, instead the cpp files should include what is neeed. Other people will probably have opionions about this, but that''s what I use and like. My header files never have any #include statements.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004755

×