0

I have internal bitOr and bitAnd functions to handle bitwise operations. It normally works alright, tested on g++ 4.7 and VC++ 2012 but failing in g++ 4.4.6 with c++0x std.

template <typename T, typename Flag_T>
inline Flag_T bitOr(T e, Flag_T flag) {
    return static_cast<Flag_T>(e) | flag;
}

To use this I do

unsigned short flag = 0x0;
flag = bitOr<MyEnum, unsigned short>(MyEnum::Val1, flag);

And I get internal g++ error, does anyone has any other work around doing bitwise operation on enum types?

In file included from main.cc:1:
enum-test.h: In function ‘Flag_T bitOr(T, Flag_T) [with T = FormatFlags,     Flag_T = short unsigned int]’:
enum-test.h:1590:   instantiated from here
enum-test.h:603: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
Preprocessed source stored into /tmp/ccKUWf68.out file, please attach this to your    bugreport.

I understand that this is compiler issue but if someone has a workaround doing bitwise operations on enums for g++ 4.4.6 (SL RHEL) it would be great as this works in other compilers. This is my own way of doing

abumusamq
  • 780
  • 1
  • 10
  • 29
  • Afaik theres nothing wrong with `flag |= MyEnum::Val1`, I don't get why all this circus just for a bitwise operation. – Havenard Jul 08 '13 at 03:02
  • @Havenard I am sure you just guessed it should work - guess what, I thought the same at first but g++ does not like it `error: no match for ‘operator|=’ in ‘flags |= (FormatFlags)2048u’` – abumusamq Jul 08 '13 at 03:12
  • Oh before strongly typed enums thats what I did, but with strongly typed enums it doesnt allow unfortunately – abumusamq Jul 08 '13 at 03:13
  • Have you tried `flag |= (unsigned short)MyEnum::Val1`? – Havenard Jul 08 '13 at 03:13
  • This worked for me but I used static_cast since I am dealing with C++, well because I am doing it in too many places while determining format I have created a seperate function to do it which does exactly the same behind the scenes. and most of all its inline(d) so overhead of function call is something I am not worried about (at least in most of compilers) – abumusamq Jul 08 '13 at 03:17
  • Nevermind, aparently strongly typed enums are a very different beast from what Im used to that for some reason prevent their silent convertion to `int` at all costs. But this answer here, http://stackoverflow.com/questions/8357240/how-to-automatically-convert-strongly-typed-enum-into-int not the accepted one but the one with 10 upvotes, seems to workaround this. – Havenard Jul 08 '13 at 03:19
  • This appears to be a [known bug][1] in gcc 4.4, fixed in gcc 4.5.1. Any chance you can upgrade your compiler? [1]http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37946 – MtnViewJohn Jul 08 '13 at 03:23
  • Yup I use gcc 4.7 mainly but this is for lib so I wouldn't know if user is using gcc 4.4. Well I am looking at other ways and will push an update here if I come up with anything. – abumusamq Jul 08 '13 at 03:25
  • 2
    Every question about C++11 scoped enums gets someone claiming it works until they realise they don't know how scoped enums work :-\ – Jonathan Wakely Jul 08 '13 at 13:58
  • @MtnViewJohn, that bug is fixed in GCC 4.4.1 not 4.5.1, so it's unlikely to be the same issue with the OP's 4.4.6 compiler – Jonathan Wakely Jul 08 '13 at 14:05

0 Answers0