casting int's to an enum

Discussion in 'C++' started by Angus, Nov 28, 2009.

  1. Angus

    Angus Guest

    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.
    Angus, Nov 28, 2009
    #1
    1. Advertising

  2. On Nov 28, 10:06 am, Angus <> wrote:
    > 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);
    Gert-Jan de Vos, Nov 28, 2009
    #2
    1. Advertising

  3. Angus

    Jonathan Lee Guest

    On Nov 28, 6:00 am, Gert-Jan de Vos <gert-
    > wrote:
    > 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
    Jonathan Lee, Nov 28, 2009
    #3
  4. Angus

    Angus Guest

    On 28 Nov, 15:06, Jonathan Lee <> wrote:
    > On Nov 28, 6:00 am, Gert-Jan de Vos <gert-
    >
    > > wrote:
    > > 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


    Or just call it unknown type if invalid type found.
    Angus, Nov 28, 2009
    #4
  5. On Nov 28, 11:06 pm, Jonathan Lee <> wrote:
    > On Nov 28, 6:00 am, Gert-Jan de Vos <gert-
    >
    > > wrote:
    > > 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


    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
    Active Volcano, Nov 29, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ghostdog
    Replies:
    2
    Views:
    300
    Percy
    Sep 15, 2003
  2. Seabook
    Replies:
    6
    Views:
    30,694
    Pete Becker
    Nov 6, 2003
  3. Schnoffos
    Replies:
    2
    Views:
    1,209
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,629
    Old Wolf
    Jan 20, 2004
  5. enum type int or unsigned int?

    , Apr 13, 2006, in forum: C Programming
    Replies:
    10
    Views:
    1,887
    Michael Mair
    May 2, 2006
Loading...

Share This Page