casting int's to an enum

A

Angus

With the following code:

enum testenum { in, out, append };

int val = 17;

testenum myenum = static_cast<testenum>(val);

Using my compiler it looks like myenum is given a value of 17! What
strategies do you suggest for dealing with this problem. One
possibility is to do this:

enum testenum { in, out, append, testmax };
int val = 17;
if(val > testenum ::testmax)
//error

testenum myenum = static_cast<testenum>(val);

Any other ideas?

Reason I ask is enum value is taken from a message which could contain
incorrect information.
 
G

Gert-Jan de Vos

With the following code:

        enum testenum { in, out, append };

        int val = 17;

        testenum myenum = static_cast<testenum>(val);

Using my compiler it looks like myenum is given a value of 17!  What
strategies do you suggest for dealing with this problem.  One
possibility is to do this:

enum testenum { in, out, append, testmax };
int val = 17;
if(val > testenum ::testmax)
    //error

testenum myenum = static_cast<testenum>(val);

Any other ideas?

Reason I ask is enum value is taken from a message which could contain
incorrect information.

You want runtime validation of a large range of values (ints) to a
small range and make a well defined mapping. Like this:

testenum get_testenum(int value)
{
switch (value)
{
case in: return in;
case out: return out;
case append: return append;
}
throw std::runtime_error("bad enum conversion");
}

I need that often in my code and I have a template:

template <typename Dst, typename Src> Dst enum_cast(Src en);

For which I make a specialization for all the conversions I need.
I have some macros:

BEGIN_ENUM_MAP(src, dst)
ENUM_ENTRY(src, dst)
END_ENUM_MAP()

That generate the specializations and switch/case code. I normally use
it to convert between enums but this works too:

int i = enum_cast<int>(append);
testenum e = enum_cast<test_enum>(1);
std::string s = enum_cast<std::string>(out);
 
J

Jonathan Lee

You want runtime validation of a large range of values (ints) to a
small range and make a well defined mapping. Like this:

testenum get_testenum(int value)
{
    switch (value)
    {
    case in: return in;
    case out: return out;
    case append: return append;
    }
    throw std::runtime_error("bad enum conversion");

}

Agreed. Only change I would (possibly) make would be to return an
"enumInvalid" value instead of throwing. Usually I define this to
be -1. But that's only if you _expect_ or are going to handle the
invalid conversions.

--Jonathan
 
A

Angus

Agreed. Only change I would (possibly) make would be to return an
"enumInvalid" value instead of throwing. Usually I define this to
be -1. But that's only if you _expect_ or are going to handle the
invalid conversions.

--Jonathan

Or just call it unknown type if invalid type found.
 
A

Active Volcano

Agreed. Only change I would (possibly) make would be to return an
"enumInvalid" value instead of throwing. Usually I define this to
be -1. But that's only if you _expect_ or are going to handle the
invalid conversions.

--Jonathan

It seems that you want to change the type of creating file.
You need to give a default prameter. I recommend this type:

testEnum getFileCreat(int value)
{
testEnum ret = in;

switch (value)
{
case in:
ret = in;
break;

case out:
ret = out;
break;

case append:
ret = append;
break;

default:
ret = in;
}

return ret;
}


also,you need pay attention to the enum{in, out, append},
the value of enum is same with the std::in,std::eek:ut,std::append
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top