streaming enum

T

toton

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
 
J

John Harrison

toton said:
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
 
T

toton

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
 
D

dasjotre

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.

#define NAMEOF(X) #X

ostream << NAMEOF(PRINTED)

this is the only way I can think of
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

toton said:
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

You can write a tool that reads a file with your enum values and maybe some
additional information and generates the code for the enum and the operator
<< . This can be much cleaner and less error prone than a combination of
templates and macros.
 
M

Marcus Kwok

toton said:
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 ...

There is no standard library or flag that you can pass to ostream for
this, but there is a discussion of this issue here with several
alternatives for implementation:

http://www.comeaucomputing.com/techtalk/#enumtostring
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top