Article View: comp.std.c++
Article #3693Re: String value of enum
From: Russell Gold
Date: Sat, 16 Mar 1996 09:47
Date: Sat, 16 Mar 1996 09:47
53 lines
2946 bytes
2946 bytes
Max Welton wrote:
>
> mtm4@rsvl.unisys.com (Michael McCormick) wrote:
> >"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++.
>
> It's not _that_ unusual. Ada has had this feature starting with
> Ada-83 (the 'image attribute).
Very true; however, Ada also gurantees some things about scalar
values that do not apply to C++ enums:
1. You can determine the order of a scalar value in its sequence
2. AFAIK, no two members of the same scalar type can have the same
underlying representation.
As a result, Ada lets you use a scalar type as an array index,
and compute the successor / predecessor from a given value. In
C++, you cannot do this. For example:
enum Code { ready=0, idle = 0, run, halt };
const char *image( Code value );
how would you handle:
cout << image( (Code) 0 );
Now if we wanted to guarantee the properties of uniqueness in value
and sequence in C++, we could do all of these other things, as well.
--
Russell Gold | (609) 727-2219
ADP, Inc. | goldr@bis.adp.com
201 E. Park Dr. | russgold@ACM.org
Mt. Laurel, NJ 08054 | russgold@aol.com
---
[ 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:
<3149AF56.606E@bis.adp.com>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com> <4i80om$2jo@ixnews3.ix.netcom.com>Show Full Headers
Complete Article Headers
From: Russell Gold <goldr@inet.bis.adp.com> Message-ID: <3149AF56.606E@bis.adp.com> X-Original-Date: Fri, 15 Mar 1996 12:56:38 -0500 Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 16 Mar 96 09:47:18 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: String value of enum Organization: Strategic Architecture References: <4i5sf3$89c@hermes.is.co.za> <Do81tp.H9u@rsvl.unisys.com> <4i80om$2jo@ixnews3.ix.netcom.com> X-Mailer: Mozilla 2.0 (WinNT; I) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMUqOLuEDnX0m9pzZAQEQSAF/dcOAlrJyiK/aYQq57oW62NMUbQb+gCyf 8DUXDOHx+r5frXIUQ1jBwWvBwi8Pv1WQ =3fDj