🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3668

Re: String value of enum

#3668
From: J. Kanze
Date: Thu, 14 Mar 1996 15:14
61 lines
3660 bytes
In article <Do81tp.H9u@rsvl.unisys.com> mtm4@rsvl.unisys.com (Michael
McCormick) writes:

|> "W. Dicks" <wd@isis.co.za> shared the following on 13 Mar 96 07:31:13 GMT:

|> >The system that I'm working often needs to know the string 
|> >value of an enum. e.g. enum week{MON=1, ..., SUN} it; it = 
|> >TUE;
|> >Then when this enum is passed as a parameter the function 
|> >should return the string based on the enum. For instance, 
|> >weekImage(it) will return "TUE". Is it not possible that such 
|> >a functionality can be built into enums?

|> What you are essentially asking for is language support for converting
|> a label into a string respresentation of its name.  That is a very
|> unusual feature to find in any language, since it would require the
|> compiler to place the symbolic dictionary in the executable file.  I
|> don't think there's a snowball's chance of this getting into C++.

Well, it is supported in other languages.  But I agree that it will not
be in C++, and should not be there, for an entirely different reason.
What happens to such strings when you change locale?  (In practice, they
are only good for debugging because of this.  And any decent debugger
will be able to display the information without the table in the
compiler.)

|> Since enum is by definition an integral type, why not assign values to your
|> enumerators that can be used as indexes into a string?:

|>  enum week {MON=0,TUE=4,WED=8,THU=12,FRI=16,SAT=20,SUN=24};
|>  const char * days = "MON\0TUE\0WED\0THU\0FRI\0SAT\0SUN";

|>  char const * weekImage(week day) = days[day];

Now this is going to be really fun to maintain.  What happens when the
program is prepared for internationalization, and the translater
modifies the strings?

Using:

	char const *const	weekdayNames[] = { "Monday" , ... } ;

and indexing with the original enum is considerably better.  In fact, in
this one particular case, it is probably acceptable.  I cannot imagine
any conceivable change in specification which would cause the number of
days in a week to change.  This is a special case, however.

For the general case, I wrote a template class which manages a map, and
a small parser (just intelligent to extract the names from an enum, and
ignore everything else) to generate the initialization data for the map.
-- 
James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
              -- A la recherche d'une activiti dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <KANZE.96Mar14131235@gabi.gabi-soft.fr>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>

Show Full Headers
Complete Article Headers
From: kanze@gabi-soft.fr (J. Kanze)
Message-ID: <KANZE.96Mar14131235@gabi.gabi-soft.fr>
X-Original-Date: 14 Mar 1996 12:12:35 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 14 Mar 96 15:14:00 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: String value of enum
Organization: GABI Software, Sarl.
References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com>
In-Reply-To: mtm4@rsvl.unisys.com's message of 13 Mar 96 23:40:59 GMT
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMUg3zuEDnX0m9pzZAQGpwAF9G7YayO/+a9nUhFwaqjk06pcMoK1rtqES
 L27nJ4LV6H7uRmF7qR8DmjMogqZmNU1q
 =ypVB