why are enum type conversions disallowed?

This is a discussion on why are enum type conversions disallowed? within the C++ Programming forums, part of the General Programming Boards category; when you have: Code: enum blah {blah...} dog; dog = 1; //why are type conversions disallowed here? thanks...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    why are enum type conversions disallowed?

    when you have:

    Code:
    enum blah {blah...} dog;
    
    dog = 1;    //why are type conversions disallowed here?
    thanks

  2. #2
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    an enumerated data type will be cast automatically to the appropiate type, but the reverse isnt true..there is no automatic conversion from an integer type to a enumeration type

    so using your example

    dog = static_cast<blah>(1);
    nextus, the samurai warrior

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    yes... but why? I mean, if c++ allows a bool to float conversion, why not an enum blah to int conversion?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because for a given value of an enum it will fit into an int but the reverse is not true.

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    for me i never assume an automatic conversion will take place...i rather explicitly tell the compiler that i am casting the value into another..its just a safe caution for me....
    nextus, the samurai warrior

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    typedef enum { red = 1, green = 2, blue = 3 } color;
    color c = 24;
    What should the compiler do with that? Since it can't reasonably know what to do, it doesn't allow it.

  7. #7
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by nextus
    for me i never assume an automatic conversion will take place...i rather explicitly tell the compiler that i am casting the value into another..its just a safe caution for me....
    How is it safe to cast everything? (assuming you don't refer to dynamic_cast when talking about values).

    Quote Originally Posted by Thantos
    Because for a given value of an enum it will fit into an int but the reverse is not true.
    One could argue that this isn't true for very large values of same sized float/integer types either.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> How is it safe to cast everything? (assuming you don't refer to dynamic_cast when talking about values). <<
    If you are explicitly making casts, then a) there are no assumptions about what is going on under the hood, and b) clearly you have thought about what you are doing, and hence, are doing it intentionally. It is still possible to do foolish things, but safer in the sense that it requires you to have thought about it.

    >> One could argue that this isn't true for very large values of same sized float/integer types either. <<
    You may get a very large numerical error trying to perform foolish conversions, but you will end up with a valid number (as useless as it may be). Converting from int to enum you will not necessarily end up with a valid enum value (see jlou's example).
    Last edited by Zach L.; 11-23-2004 at 07:54 AM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Quote Originally Posted by Zach L.
    >> How is it safe to cast everything? (assuming you don't refer to dynamic_cast when talking about values). <<
    If you are explicitly making casts, then a) there are no assumptions about what is going on under the hood, and b) clearly you have thought about what you are doing, and hence, are doing it intentionally. It is still possible to do foolish things, but safer in the sense that it requires you to have thought about it.

    >> One could argue that this isn't true for very large values of same sized float/integer types either. <<
    You may get a very large numerical error trying to perform foolish conversions, but you will end up with a valid number (as useless as it may be). Converting from int to enum you will not necessarily end up with a valid enum value (see jlou's example).
    nice rebuttal Zach...took the words right out of my mouth
    nextus, the samurai warrior

  10. #10
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    Quote Originally Posted by jlou
    Code:
    typedef enum { red = 1, green = 2, blue = 3 } color;
    color c = 24;
    What should the compiler do with that? Since it can't reasonably know what to do, it doesn't allow it.
    well lets work on your example...enum does allow ranges...so if you have

    Code:
    enum colors { red = 1, blue = 10 };
    colors myColor = static_cast<colors>(5); //perfectly legal!
    nextus, the samurai warrior

  11. #11
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Zach L.
    If you are explicitly making casts, then ..., and b) clearly you have thought about what you are doing, and hence, are doing it intentionally.
    I wouldn't bet on that one, especially not when falling into the habit of always (as in "preemptive") using casts. :-) I see your point though.

    Quote Originally Posted by Zach L.
    Converting from int to enum you will not necessarily end up with a valid enum value (see jlou's example).
    Yes, but do you have to have? If it's an invalid enum all that happens is that your entire switch or whatever you use to test the cases fails. If you get a completely wrong number after converting an integer type to a ("more precise") floating point you might (or might not) be in worse troubles, especially since this comes completely unexpected.
    Anyway, I realize there is no point in discussing what the language should allow and what not.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, if you are in the habit of preemptive casting, then it is a good idea to get out of it. If you are casting too often, then there is probably a better way of doing things.

    Why you would not want to cast an int to an enum really doesn't have to do with what will fit where (that is a practical consideration, though), but really what you are dealing with is a violation of an abstraction. If you want an arbitrary integer, use an int. If you want something from a set list of items that have some other inherent meaning, use an enum.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 11:35 PM
  4. Type Conversions
    By ew16301 in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2005, 12:18 AM
  5. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21