Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: enum count

Reply
Thread Tools

Re: enum count

 
 
Klaus Eichner
Guest
Posts: n/a
 
      07-26-2003
"Clive" <(E-Mail Removed)> wrote in message
news:3f21e5cc$0$23611$(E-Mail Removed). au...
> If you have an enum, is there any way during execution to find the number

of
> values in the enum?
> Say I have,
>
> enum great { five, ten, fifteen };
>
> How could I get the number 3 from that?


You could get the number 3 from 'great' with your own, user-defined
'Enum_Info' template:

cout << "The number of values in enum great is "
<< Enum_Info<great>::number_of_elements
<< endl;

The 'Enum_Info' template is defined as follows.
Suppose you have the following enums:

enum great { five, ten, fifteen };
enum greater { none, one, fourtytwo, fourtythree, fourtyfour };
enum even_greater { minusone, minustwo, minusthree, minusfour, minusfive,
minussix, minusseven };

You could build a template class 'Enum_Info' which uses specialisation to
register the number of elements in each enum

template <class T> class Enum_Info { };
template <> class Enum_Info<great> { static const int number_of_elements =
3; };
template <> class Enum_Info<greater> { static const int number_of_elements =
5; };
template <> class Enum_Info<even_greater> { static const int
number_of_elements = 7; };



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments