From: mario semo on
Hello,

is there any way to force the compiler to take an enum as 1 byte - at least
for a specific enum? my problem is, that i have a persistent structure,
written by another compiler, and everything works fine, except the enums.
The problem is, that i cannot change the persistent data layout. of course,
i can read the structure element by element, and read a char instead of the
enum, .... but i would prefer to tell the compiler to take this enum as a
byte.

Sample:

#include <stdio.h>

#define CHK(a) printf("sizeof(%s)=%d\n",#a,sizeof(a));

enum TestEnum
{
en1,
en2
};

struct TestStruct
{
int i1;
TestEnum t1;
TestEnum t2;
};

int main(int argc,char *argv[])
{

CHK(TestEnum);
CHK(TestStruct);

return 0;
}

VC++ :
sizeof(TestEnum)=4
sizeof(TestStruct)=12

OtherCompiler :
sizeof(TestEnum)=1
sizeof(TestStruct)=8


--
mit freundlichen Gr��en

mario semo

From: Giovanni Dicanio on

"mario semo" <mario_semo(a)hotmail.com> ha scritto nel messaggio
news:E82DB684-05C5-4C00-AA95-596439347C30(a)microsoft.com...

> is there any way to force the compiler to take an enum as 1 byte - at
> least for a specific enum?

I think it is not possible in C++...


> struct TestStruct
> {
> int i1;
> TestEnum t1;
> TestEnum t2;
> };

You may consider using a typedef and #define's to "simulate" the 1-byte
enum, e.g.

// 1-byte enum
typedef BYTE TestEnum;

// Values
#define en1 0
#define en2 1

BTW: I would "protect" the scope of enum constants, using a proper prefix
for them, e.g.
(#define <enum name>_<enum constant> <value>)

#define TestEnum_en1 0
#define TestEnum_en2 1
...


HTH,
Giovanni


From: QbProg on
enum Test : unsigned char
{
A = 1,....
....
};

or
enum Test : <any ordinal type you want>
{

};


it's a VC++ extension

QbProg
From: Carl Daniel [VC++ MVP] on
mario semo wrote:
> Hello,
>
> is there any way to force the compiler to take an enum as 1 byte - at
> least for a specific enum? my problem is, that i have a persistent
> structure, written by another compiler, and everything works fine,
> except the enums. The problem is, that i cannot change the persistent
> data layout. of course, i can read the structure element by element,
> and read a char instead of the enum, .... but i would prefer to tell
> the compiler to take this enum as a byte.

There's no standard way to do that. The compiler is free to choose any
integer type that's large enough to hold the range of values.

VC++ 2005 and later support a non-standard extension that lets you specify
the storage used for the enum:

enum [tag] [: type] {enum-list} [declarator];

type is the underlying type of the identifiers. This can be any scalar type,
such as signed or unsigned versions of int, short, or long. bool or char is
also allowed.

http://msdn.microsoft.com/en-us/library/2dzy4k6e.aspx

-cd


From: Ondrej Spanel on
We use a template like this (real code is somewhat longer and more
complicated to maintain GNU C compatibility):

/// enum stored using any type instead of default unsigned int
template <class Enum, class Type=unsigned char>
class SizedEnum
{
Type _data;

public:
operator Enum () const {return (Enum)_data;}
SizedEnum( Enum val ):_data(val){}
SizedEnum(){}
};

Cheers
Ondrej

mario semo napsal(a):
> Hello,
>
> is there any way to force the compiler to take an enum as 1 byte - at
> least for a specific enum? my problem is, that i have a persistent
> structure, written by another compiler, and everything works fine,
> except the enums. The problem is, that i cannot change the persistent
> data layout. of course, i can read the structure element by element, and
> read a char instead of the enum, .... but i would prefer to tell the
> compiler to take this enum as a byte.
>
> Sample:
>
> #include <stdio.h>
>
> #define CHK(a) printf("sizeof(%s)=%d\n",#a,sizeof(a));
>
> enum TestEnum
> {
> en1,
> en2
> };
>
> struct TestStruct
> {
> int i1;
> TestEnum t1;
> TestEnum t2;
> };
>
> int main(int argc,char *argv[])
> {
>
> CHK(TestEnum);
> CHK(TestStruct);
>
> return 0;
> }
>
> VC++ :
> sizeof(TestEnum)=4
> sizeof(TestStruct)=12
>
> OtherCompiler :
> sizeof(TestEnum)=1
> sizeof(TestStruct)=8
>
>