1

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?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Justin
  • 45
  • 1
  • 4

1 Answers1

2

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+ (https://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.

Community
  • 1
  • 1
Cliff Bilbrey
  • 309
  • 1
  • 8
  • Thank you Cliff for your answer. Would you know by any chance how to add compiler flag in Work bench? I just want to see a possibility of adding a directive “-std=c++11” so WR might be able to build? I really appreciate your help. --Justin – Justin Oct 17 '16 at 18:01
  • I don't have WorkBench installed at the moment. But, this answer to another question describes how to find and modify compiler flags in the active build spec: http://stackoverflow.com/a/8593028/1204259 – Cliff Bilbrey Oct 17 '16 at 19:01
  • Weird. That didn't link as I would've expected. I was trying to link to other answer, which describes the active build spec settings that can be found here: Properties->Build Properties->Build Support and Specs. Anyway, you should be able to find the active compiler and experiment with flags inside that spec. Good luck! – Cliff Bilbrey Oct 17 '16 at 19:04
  • 2
    @Justin Workbench 3.3 ships with gcc 4.3.3 (I believe, certainly WB 3.3.6 only has 4.3.3, so WB 3.3 will be 4.3.3 **or lower**. C++11 was not added to GCC untill 4.7, and even then only experimental support. If you specifiy -std=c++11 you will just get a compiler error. – mjs Oct 26 '16 at 08:58
  • @mjs thanks for your comment. Can you think of any way to associate datatype with enumeration? Thanks. – Justin Oct 27 '16 at 15:24
  • @Justin - Im afraid not, in a language that doesn't support it. I think the solution provided is about the best you will get - and so i got my upvote. – mjs Oct 28 '16 at 11:19
  • After a bit more digging, I thought that I might have been onto something with boost scoped_enum (http://www.boost.org/doc/libs/1_62_0/libs/core/doc/html/core/scoped_enum.html), using underlying types. But, that's going to make use of C++03 strongly-typed enums, which you won't have either (https://gcc.gnu.org/gcc-4.3/cxx0x_status.html). Unfortunately, I think any good solution to your problem is going to require a more modern compiler. – Cliff Bilbrey Oct 30 '16 at 16:17