On Feb 13, 12:54 pm, John Harrison <john_androni...@hotmail.com>
wrote:
toton wrote:
Hi,
I have a bunch of enums, which I want to stream with their names
rather than the enum value (just like bool has a facility in the
stream)
Any facility available in standard library / boost ?
something like,
enum TYPE{
PRINTED,CURSIVE,MIXED
};
and
ostream << PRINTED will print "PRINTED" if some flag is set to
ostream ...
abir
No standard facility. The problem is that like any variable name the
names of the enums no longer exist when your program has been compiled.
Of course you can just write
ostream& operator<<(ostream& out, TYPE t)
{
switch (t)
{
case PRINTED:
out << "PRINTED";
...
}
}
You could even make this dependent on a flag in ostream if you wanted.
john
This is one which I use at present. That one is needed for all enum
and need a default case.
The names do not exist at run time (except rtti, or any kind of
extended rtti in that matter) What I was thinking is that, the names
are always available at the compile time. Thus a template based
opetartor<< which generates the above function spitted in its parts
(specialization ) can do that trick, just like the marco based rtti
used in most of the common frameworks like Qt , VCF etc (which used
some meta class to store this info even at the run time). Only my need
may be generating a bunch of such functions at the compile time, than
making enum heavy with the meta classes.
Thanks
abir