VC++ 에서 Enumeration 크기 지정하는 법.
글쓴이: parrier / 작성시간: 목, 2008/09/04 - 6:59오후
일반적으로 VC++로 컴파일하게 되면 Enumeration으로 4byte가 할당되게 됩니다.
헌데 ARM Compiler는 해당 내용에 맞게 1,2,4(bytes)로 바뀌게 되지요.
enumeration 선언을 다음과 같이 한다면
typedef enum {
a,
b,
c,
d
}
이것을 VC++에서 컴파일한다고 했을 때, 1,2,4byte로 컴파일하려고 한다면 어떤
방법 or 편법을 쓸 수 있을까요?
Forums:


그런 기능은 없는
그런 기능은 없는 걸로 알고 ARM이 sizeof(enum)을 4byte로 만드는게 나을 거 같네요
ADS 1.2의 ADS_CompilerGuide_D.pdf 화일을 보시면
Controlling implementation details 챕터에서
-fy
This option forces all enumerations to be stored in integers. This option
is switched off by default and the smallest data type is used that can hold
the values of all enumerators
-fy를 주시면 됩니다
T_T ARM 쪽에는 손을 댈 수가 없어서 그렇답니다 ;.;
ARM 쪽에는 손을 댈 수가 없구요. C++로는 편법으로 크기를 정하는 방법이 있던데
// FixedEnum.h
// By James M. Curran
template
class FixedEnum
{
SizeType fe;
public:
FixedEnum(Enum e): fe(static_cast(e)) {}
FixedEnum(): fe(static_cast(Enum())) {}
operator Enum() const { return static_cast(fe);}
SizeType asNative() {return fe;}
};
#include "FixedEnum.h"
#include
using std::cout;
using std::endl;
enum count {zero, one, two, three, four, thousand=1000};
FixedEnum counter;
// should be 1 byte & work like an enum.
int main(int argc, char* argv[])
{
counter = two;
cout << "Counter is " << sizeof(counter) << "byte(s) long, with a value of"
cout << counter << endl;
// counter = 5;
// would cause compile-time error.
counter = thousand; // warning - works, but not with expected result
}
-------------------------------------------------------------------------------
위와 같이 되어 있습니다만, 제 C++을 몰라서 C로 어떻게 고쳐야 할 지, 또 위 구문이 C문법으로 고치는 것이
가능한지 모르겠네요 ;.;
-_-v
다시 고칩니다.
#ifdef 1BYTE typedef ENUM_X unsigned char; #elif 2BYTE typedef ENUM_X unsigned short; #else typedef ENUM_X unsigned int ; #endif typedef enum{ A, /* use it by enum_a */ B, /* use it by enum_b */ C, /* use it by enum_c */ D /* use it by enum_d */ } __X; #define enum_a ((ENUM_X)A); #define enum_b ((ENUM_X)B); #define enum_c ((ENUM_X)C); #define enum_d ((ENUM_X)D); #endif잘 될지는 저도 의문입니다. -_-;
type 으로 쓸때는 ENUM_X
value 로 쓸때는 enum_a ~ enum_d;
Neogeo - Future is Now.
C++ enum 확장을 쓰시면
C++ enum 확장을 쓰시면 될 것 같습니다. (제가 당장 VC++를 쓸 환경이 안 되기 때문에 확인은 못 하겠군요.) C 모드에서는 지원하지 않는다는 걸 유의하시고... 문법은 중괄호 시작 앞에 클래스 상속과 같이 ": int" 같은 걸 넣는 방법입니다.