Enumerations as case labels

M

Martin Magnusson

I'm using enums as case labels throughout my code, and that works fine
in gcc 3.3.4. However, when compiling it with the compiler that ships
with Dev-C++ 4.9.8.0, I get the error "case label does not reduce to an
integer constant".

The case label in question looks like this
case I<D>::E:

And I have a class
template <unsigned D>
class I
{
public:
enum M {A,B,C,D,E};
};

Shouldn't that work?
 
V

Victor Bazarov

Martin said:
I'm using enums as case labels throughout my code, and that works fine
in gcc 3.3.4. However, when compiling it with the compiler that ships
with Dev-C++ 4.9.8.0, I get the error "case label does not reduce to an
integer constant".

The case label in question looks like this
case I<D>::E:

And I have a class
template <unsigned D>
class I
{
public:
enum M {A,B,C,D,E};
};

Shouldn't that work?

It seems that the line

case I<D>::E:

should only work inside the class member function (since there probably
no 'D' outside, although, who know?). However, your class template has
no member functions. Care to post a complete example?

V
 
P

Pete Becker

Martin said:
I'm using enums as case labels throughout my code, and that works fine
in gcc 3.3.4. However, when compiling it with the compiler that ships
with Dev-C++ 4.9.8.0, I get the error "case label does not reduce to an
integer constant".

The case label in question looks like this
case I<D>::E:

And I have a class
template <unsigned D>
class I
{
public:
enum M {A,B,C,D,E};
};

Shouldn't that work?

template <> class I<int>
{ // explicit specializatino
};

surprise: no E!
 
M

Mark A. Gibbs

Martin said:
I'm using enums as case labels throughout my code, and that works fine
in gcc 3.3.4. However, when compiling it with the compiler that ships
with Dev-C++ 4.9.8.0, I get the error "case label does not reduce to an
integer constant".

The case label in question looks like this
case I<D>::E:

And I have a class
template <unsigned D>
class I
{
public:
enum M {A,B,C,D,E};
};

Shouldn't that work?

unless i'm mistaken it should not. you are using D for both the template
parameter and the enum member name. if you want that 4th enum member to
equal D, you should do:

template <unsigned N>
class I
{
public:
enum M { A, B, C, D = N, E };
};

secondly, as others have pointed out unless there is another D, you
can't access D outside of the template. this should work:

case I<42>::E:

or maybe:

int const D = 42;
case I<D>::E:

i'm not sure what you're trying to do, but if you want to use the D enum
member as the template parameter, you can't do it quite like that. you
could follow the example of ios in the stl and do:

class I_base
{
public:
enum M { A, B, C, D, E };
};

template <unsigned N>
class I :
public I_base
{
public:
};

and then you could do:

case I<I_base::D>::E:

if that's what you're looking for.

--
Mark A. Gibbs (aka. Indi)
Administrator
#c++ on irc.Rizon.net

http://ca.geocities.com/[email protected]/
(temporary website)
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top