Justin Justin - 1 month ago 13
C++ Question

Enum with datatype in WindRiver Workbench 3.3

In Visual Studio 2010, I was able to build enumeration with datatype just fine.

enum FRUIT_E : UINT16
{
APPLE = 0,
LEMON = 1,
GRAPE = 2,
};


However, when I tried to compile in WR Workbench, I get the following error:


: error: use of enum 'FRUIT_E' without previous declaration


I really need to specify the datatype of enum as fields are bitpacked. Is there any way I could explicitly specify the type of enumeration?

Answer

The short answer to your question is no.

The ability to define a base type for enumerations is a language feature that was not added until C++11 (http://en.cppreference.com/w/cpp/language/enum). Unfortunately, WindRiver has been slow to support compilers that comply with modern C++ standards (C++ 11 in vxworks). There is mention of support for C++11 in commercial versions of g++ for VxWorks 7.0+ (http://stackoverflow.com/a/36311473). But, your WorkBench version (<4.0) implies that this won't be helpful to you.

In your situation, I might cobble together a work-around, such as this:

namespace FRUIT_E
{
    static const UINT16 APPLE = 0;
    static const UINT16 LEMON = 1;
    static const UINT16 GRAPE = 2;
}

typedef FRUIT_T UINT16;

...

FRUIT_T fruit = FRUIT_E::APPLE;

However, this won't provide type safety, as FRUIT_T is just an alias for UINT16. So, it's far from ideal. I've added tags to your question. There may be better work-arounds, emulating the desired behavior more closely. But, the addition of a new language feature would seem to indicate that existing methods were inadequate.

Comments