Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ Programming
➜ General
➜ Templates and warning switches
Templates and warning switches
|
Posting of new messages is disabled at present.
Refresh page
Pages: 1 2
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Tue 06 Sep 2005 07:23 PM (UTC) |
Message
| Just for the hell of it, I was going over my code with the -Wswitch-default flag and have run into one warning I cannot resolve:
mud_prog.h: In function 'void mprog_file_read(N*, char*)':
mud_prog.h:173: warning: switch missing default case
Yes, this is a c++ templated function, which looks like:
template< class N > void mprog_file_read( N *prog_target, char *f )
{
mud_prog_data *mprg = NULL;
char MUDProgfile[256];
FILE *progfile;
char letter;
snprintf( MUDProgfile, 256, "%s%s", PROG_DIR, f );
if( !( progfile = fopen( MUDProgfile, "r" ) ) )
{
bug( "%s: couldn't open mudprog file", __FUNCTION__ );
return;
}
for( ; ; )
{
letter = fread_letter( progfile );
if( letter == '|' )
break;
if( letter != '>' )
{
bug( "%s: MUDPROG char", __FUNCTION__ );
break;
}
mprg = new mud_prog_data;
mprg->type = mprog_name_to_type( fread_word( progfile ) );
switch( mprg->type )
{
default:
mprg->arglist = fread_string( progfile );
mprg->comlist = fread_string( progfile );
mprg->fileprog = true;
prog_target->progtypes.set( mprg->type );
prog_target->mudprogs.push_back( mprg );
break;
case ERROR_PROG:
bug( "%s: mudprog file type error", __FUNCTION__ );
deleteptr( mprg );
continue;
case IN_FILE_PROG:
bug( "%s: Nested file programs are not allowed.", __FUNCTION__ );
deleteptr( mprg );
continue;
}
}
FCLOSE( progfile );
return;
}
Is there any way to silence the warning properly since it's pretty obvious there *IS* a default case? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Tue 06 Sep 2005 07:30 PM (UTC) |
Message
| Try putting the default after the two cases? *shrug* Got me beat on that one...
This kind of thing would be better done with object superclasses than templates, though. Generates less code, in any case. But that has nothing to do with your error... |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Greven
Canada (835 posts) Bio
|
Date
| Reply #2 on Tue 06 Sep 2005 07:44 PM (UTC) |
Message
| Only thing that I can think of is that the two lower statements do not have break statements, and the compiler is having issues with it. Don't know how a switch statement deals with continue(even though we know the continue is for the loop and would break out of the switch anyways). Do you need breaks after the continues? It will never execute, but the compiler flag may require it and not take into consideration a continue statement. |
Nobody ever expects the spanish inquisition!
darkwarriors.net:4848
http://darkwarriors.net | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #3 on Tue 06 Sep 2005 07:56 PM (UTC) |
Message
| That could be it, although I'd be a little surprised if the compiler was so 'silly' as to not "see" the default case. But, it's worth a try. But then will it generate unreachable code warnings? :P |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #4 on Tue 06 Sep 2005 09:36 PM (UTC) |
Message
| No, it didn't generate unreachable code warnings. And I had the default case under the other two before I posted. I've even changed the continue; statements to break; statements with no change. Maybe a compiler bug? :P
You mentioned this being better done with an object superclass. Could you explain that and provide an example? | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #5 on Tue 06 Sep 2005 09:46 PM (UTC) |
Message
| So putting in breaks worked? Funky. :) What version of gcc do you have?
Well, my guess is that you're trying to unify the handling of oprogs, mprogs and rprogs. What could be done is to have all three of those structures inherit from a single 'prog' structure, one that contains all common variables e.g. arglist, proglist, fileprog...
It's not a big deal, really. Personally I try to do that kind of thing just because it makes things more organized (IMO, at least). |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #6 on Tue 06 Sep 2005 10:29 PM (UTC) Amended on Tue 06 Sep 2005 10:40 PM (UTC) by Samson
|
Message
| No, putting in breaks didn't change anything, it still complains.
As far as the superclass thing, ok, so you're talking about inheritence then. I haven't messed with that yet, but this looks like a good time to try I suppose. :)
EDIT:
Er... nevermind. I just remembered why this was done this way. mud_prog_data is all common to mobs/objs/rooms. It's the lists on each, mob_index, obj_index, and room_index that caused me to write this template.
prog_target is either a mob, obj, or room index. And if you look at how mprog_file_read is working, the function will attach multiple scripts to one of these from a single file, so intereting won't really help here.
I guess I'll just leave this warning switch out now that I've fixed all the other legitimate places it was complaining. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #7 on Tue 06 Sep 2005 10:37 PM (UTC) |
Message
| Yeah, I was talking about inheritance. There are some potential headaches but for this kind of relatively simple setup, it's not too bad.
As for the original problem, it's hard for me to replicate your situation exactly, but I tried the following:
Quote:
#include <iostream>
using namespace std;
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int i = 0;
for ( ;; )
{
switch ( arr[i] )
{
default:
cout << arr[i] << endl;
break;
case 1:
case 2:
case 3:
case 4:aybe your version of gcc is confused?
case 5:
case 6:
case 7:
case 8:
case 9:
cout << "woot" << endl;
continue;
}
i++;
}
return 0;
}
I then compiled with g++ version 3.4.2 on Cygwin:
Quote:dhaley@muri5 ~
$ g++ -o test test.cpp -Wswitch-default -Wall --pedantic
dhaley@muri5 ~
$
No warnings or errors...
(OK, I know this isn't the most scientific of tests, since a lot of things are different, but still...) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #8 on Tue 06 Sep 2005 10:50 PM (UTC) |
Message
| I also tried with the pedantic flag, no difference. This template is the only thing it's complaining about. Your test isn't using a template, so I'm not surprised it worked. Take out your default: tag and retest, it should complain. | Top |
|
Posted by
| Dralnu
USA (277 posts) Bio
|
Date
| Reply #9 on Tue 06 Sep 2005 10:53 PM (UTC) |
Message
| This may seem an odd place to ask this, but what would it take to convert C to C++? I'm thinking of moving over to C++ myself. Sorry this may be an odd place to ask, but since you're talking about it... | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #10 on Tue 06 Sep 2005 10:56 PM (UTC) Amended on Tue 06 Sep 2005 10:57 PM (UTC) by David Haley
|
Message
| OK... now I have this:
Quote:#include <iostream>
using namespace std;
template <typename ArrayType>
void foo( ArrayType * arr )
{
int i = 0;
for ( ;; )
{
switch ( arr[i] )
{
default:
cout << arr[i] << endl;
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
cout << "woot" << endl;
continue;
}
i++;
}
}
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
foo<int>(arr);
return 0;
}
Compiled with g++ version 3.4.2 on Cygwin:
Quote:dhaley@muri5 ~
$ g++ -o test test.cpp -Wswitch-default -Wall --pedantic
dhaley@muri5 ~
$
Again, no warnings or errors.
You are right though that when I remove 'default' I definitely get the error.
What version of gcc are you using? |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Flannel
USA (1,230 posts) Bio
|
Date
| Reply #11 on Wed 07 Sep 2005 12:55 AM (UTC) |
Message
| continue (as opposed to break) is not a valid statement inside of a switch body. (why would you put a continue in there anyway?)
I'll bet the problem is with GCC expecting to see default after all the rest of the cases, which is customary, but (apparently) not required by the standard. |
~Flannel
Messiah of Rose
Eternity's Trials.
Clones are people two. | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #12 on Wed 07 Sep 2005 12:58 AM (UTC) |
Message
| If it's a switch statement in isolation, then sure, it's not valid... but this is a continue statement inside a switch inside a for loop, so it's perfectly valid.
It might just be a gcc bug that was fixed in later versions. I can't reproduce the error, but that doesn't necessarily say a lot since I didn't reproduce the entire situation. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Flannel
USA (1,230 posts) Bio
|
Date
| Reply #13 on Wed 07 Sep 2005 01:52 AM (UTC) |
Message
| Oh, right, but still, why would you put a continue there, when a break would work just as well? There's nothing after the switch statement, does it compile better?
Anyway, the 'more correct' way to use continues would be to include a break after the continue, for each case; this way if you want to remove the continue statement (for debugging, or if you want to do something after freeing memory) you'll still have the correct behavior (as opposed to deleting it once for ERROR_PROG and again for IN_FILE_PROG. |
~Flannel
Messiah of Rose
Eternity's Trials.
Clones are people two. | Top |
|
Posted by
| Samson
USA (683 posts) Bio
|
Date
| Reply #14 on Wed 07 Sep 2005 03:31 AM (UTC) |
Message
| The original function I modified had the continue statements there, I changed it after realizing they looked silly. It didn't make any difference at all with regard to this error.
I am using GCC 4.0.1 right now, so I guess I need to get this tested under an older version to see what kind of results come back from it. I do have to wonder if I've stumbled onto some kind of odd compiler bug :P | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
52,538 views.
This is page 1, subject is 2 pages long: 1 2
Posting of new messages is disabled at present.
Refresh page
top