James Kanze
9/26/2008 1:26:00 PM
On Sep 26, 10:37 am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.it> wrote:
> tony_in_da...@yahoo.co.uk wrote:
> > - Someone could post on here "how do I stream the symbolic
> > identifier associated with an enum?" and 20 people would
> > leap on it merrily saying "you can't", or proposing arcane,
> > inconvenient hacks. Then go on to discuss whether a
> > semicolon or a comma is more appropriate at some point in a
> > footnote in the Standard.
> Probably it's because you can't.
You can't is pretty strong language. The standard doesn't
forbid extensions, so you could always write your own C++
compiler, with some sort of support for this as an extension.
(Let us know when you're finished.) Short of that, of course...
> > - I say, "hey, here's a trick so you can say `ENUM(Colour,
> > Red = 4, Green = 39, Blue = Red|Green);' then "cout <<
> > some_colour;" will actually print the identifier, and
> > _nobody cares_. Not the people who've been asking. Not the
> > people who've been answering.
> Probably because it's a macro-workaround and not what intended
> in the original question "how do I stream the symbolic
> identifier associated with an enum", whose correct answer is
> "you can't". It's plenty of these macro facilities in
> different libraries out there.
Yes, it's an ugly hack that no one would want to use. The
solution is a modified compiler, or failing that, an external
program which parses C++ and generates whatever is needed.
There's one at my site (kanze.james.neuf.fr/code-en.html -- the
program is called enumgen), for those who are interested,
although it's an early version, which generates code which
requires the use of my library (mainly Fallible). The version I
use has options for the generated code: returns Fallible,
returns a pointer, just generates an std::map (which means
dynamic initialization, rather than static), or uses a user
provided template (in the general English sense, not a C++
template). It also supports a number of transformations on the
symbolic name, which can be useful in specific contexts. (It
also can be used to generate an iterator over the enum, ++ and
-- operators or |, |=, & and &= operators.)
Of course, if you're not interested, I'm not going to cry about
it either. It's there, for those who are interested, and that's
it. (The up to date version will be uploaded one of these days.
Once I get things working under Windows again.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34