J
James Brown
Hi,
I have the following enum declared:
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ };
What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs
(this is
why I started the TOKEN enum off at '1000' so I had plenty of space at the
start....and I don't really want to type out 127 values into my enum
declaration....
So this is what I would like to do in my code:
TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = TOKEN(5); // compiles ok but maybe undefined behaviour??
int ch = 'X';
TOKEN t4 = TOKEN(ch); // compiles but I think it's illegal???
could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok? I read the section 29.18 in the
FAQ
which says:
"But be sure your integer is a valid enumeration value.
If you go provide an illegal value, you might end up with something other
than
what you expect. The compiler doesn't do the check for you; you must do it
yourself"
Now I deliberately want to put an illegal value into an enum type...for my
particular
scenario this seems the neatest way to do what I want. When the FAQ says I
might end up with something unexpected, does this refer to undefined
behaviour
as described by the C++ standard or does it simply mean "be careful there is
no compile-time check for what you are trying....but it is ok at runtime???"
If I converted my TOKEN value back to an integer, would the value
"roundtrip"??
TOKEN t5 = TOKEN(5)
int num = int(t5); /* would val always contain 5 on every c++ compiler???
*/
can anybody clarify my confusion or suggest an alternate solution?
tia,
James
I have the following enum declared:
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ };
What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs
(this is
why I started the TOKEN enum off at '1000' so I had plenty of space at the
start....and I don't really want to type out 127 values into my enum
declaration....
So this is what I would like to do in my code:
TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = TOKEN(5); // compiles ok but maybe undefined behaviour??
int ch = 'X';
TOKEN t4 = TOKEN(ch); // compiles but I think it's illegal???
could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok? I read the section 29.18 in the
FAQ
which says:
"But be sure your integer is a valid enumeration value.
If you go provide an illegal value, you might end up with something other
than
what you expect. The compiler doesn't do the check for you; you must do it
yourself"
Now I deliberately want to put an illegal value into an enum type...for my
particular
scenario this seems the neatest way to do what I want. When the FAQ says I
might end up with something unexpected, does this refer to undefined
behaviour
as described by the C++ standard or does it simply mean "be careful there is
no compile-time check for what you are trying....but it is ok at runtime???"
If I converted my TOKEN value back to an integer, would the value
"roundtrip"??
TOKEN t5 = TOKEN(5)
int num = int(t5); /* would val always contain 5 on every c++ compiler???
*/
can anybody clarify my confusion or suggest an alternate solution?
tia,
James