Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 20th, 2008, 11:15 AM
hectorchu@gmail.com
Guest
 
Posts: n/a
Default Enum problem

typedef enum { A, B } E;

int main()
{
E e = A;
e |= B;
return 0;
}

g++ complains with:
test.cpp:6: error: invalid conversion from 'int' to 'E'

My question is, how do I do in C++ what C would do here?
The definition of E cannot be changed.
Any solution that works with g++ is fine, including compile flags.

Hector
  #2  
Old March 21st, 2008, 02:35 AM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: Enum problem

Andrey Tarasevich wrote:
Quote:
...
From the pedantic point of view, it is impossible in general case. In
C++ the underlying type of the enum can be arbitrarily small. It is
basically guaranteed to only have enough range to store all values in
the enum-constant list and value 0 (see 7.2. for the precise
definition). This means that an attempt to force an 'A | B' value into
an object of type 'E' can result in undefined behavior simply because it
doesn't fit into the range.
...
On the second thought, I'm wrong here. The precise definition given in
7.2 actually ensures that 'A | B' will fit into the range. 'A + B' might
not.

--
Best regards,
Andrey Tarasevich
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles