Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strongly typed enums in g++-4.4

According to the GCC C++11 support status website, strongly typed enums are available for g++4.4 and greater.

However the following does not compile with g++4.4:

enum class Foo
{
   value_1,
   value_2
};

int main()
{
   Foo a = Foo::value_1;
   Foo b = Foo::value_2;
   const bool test = ( a < b );
}

The error message is error: invalid operands of types ‘Foo’ and ‘Foo’ to binary ‘operator<’.

Compilers that accept the code include g++-4.6, g++-4.7, g++-4.8 and clang++ 3.2. ( I couldn't test with g++-4.5 as I don't have it installed currently (and Ubuntu 13 doesn't want me to))

I could easily provide a fallback for this (rather old) compiler with a macro, but I generally dislike that (where does it stop?...).

What's the problem here? Is the information of support wrong or is another bit missing that's not included in "support for strongly-typed enums"? Last option I can think of: Is the problem in my code?

like image 926
stefan Avatar asked Nov 03 '22 18:11

stefan


1 Answers

It's a known bug. As @Casey found out, originally g++-4.4 did not support any relational operations on strongly-typed enums. For equality, this was fixed in version 4.4.1, but the fix for all other relations such as < and > only made it into 4.5.1 and above.

This is the original bug thread on the gcc bugzilla: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38064

like image 70
stefan Avatar answered Nov 08 '22 04:11

stefan