For digital electronic circuit simulation, i wanted to implement Three states. i.e High, Low and Undefined in C++.
I saw Boost::tribool
, and they implement it using enum
Now for conserving some memory i have implemented it using 1 Byte Int
#ifdef _WINDOWS
typedef __int8 State;
#elif
typedef int8_t State;
#endif
const State kLow = 0;
const State kHigh = 1;
const State kUndefined = 2;
- Do you see any potential problem with it?
- And why would one waste 4 bytes instead of a single byte for 3 values only
- Do you have a still better way?
-
\$\begingroup\$ That's an 8-bit int, not a 1-byte int. \$\endgroup\$user36– user362012年01月01日 16:13:21 +00:00Commented Jan 1, 2012 at 16:13
-
\$\begingroup\$ Of course, but I was more concerned about memory usage, that's why byte. Ha, I would have loved to do this in 2 bits instead! \$\endgroup\$Vinayak Garg– Vinayak Garg2012年01月02日 06:00:34 +00:00Commented Jan 2, 2012 at 6:00
2 Answers 2
I would definitely recommend using enumerations. As this is December 2011, I would imagine your compiler supports an explicit underlying type on enumerations (GCC supported this since 4.4 and Visual C++ now supports it in 11.0). As a side note, <cstdint>
is supported by visual c++ now (as of 10.0).
As an addenum to traditional c-style enumerations:
enum state : std::int8_t {
S_UNKNWON,
S_HIGH,
S_LOW
};
Or adding scope to the enumeration's memebers (avoid polluting global scope):
enum class state : std::int8_t {
unknown,
high,
low
};
state status = state::unknown;
Of course, after looking at boost::tribool
, I would recommend that over anything -- unless you really need to ensure that state
is exactly 1 byte.
-
\$\begingroup\$ +1 for informing about cstdint. no more #ifdef required. And yeah i wanted some savings, so enum state : int8_t solved the problem, with type safety! \$\endgroup\$Vinayak Garg– Vinayak Garg2011年12月30日 06:25:24 +00:00Commented Dec 30, 2011 at 6:25
The main reason there is no accidental conversion (type safety is one of the keys to using C++ correctly).
enum Tri { Yes, No, Maybe };
int main()
{
Tri y = Yes;
y= 1; // Fails to compile.
}
Secondly you are using three bytes to hold the different states here
const State kLow = 0;
const State kHigh = 1;
const State kUndefined = 2;
With an enum there is no space taken up (though potentially the above may be optimized out).
C++11 also allows you to specify the size of an enum:
enum class Tri : char { Yes, No, Maybe };
// ^^^^ Uses a char sized object
Do you see any potential problem with it?
Yes. Not type safe
And why would one waste 4 bytes instead of a single byte for 3 values only
Why not. Does it really matter in any modern PC.
OK for embedded systems maybe (but you obviously are using WINDOWS)
Do you have a still better way?
Yes. Use enum in C++11
-
\$\begingroup\$ I got a kick out of pushing you over the 5K border ^^ \$\endgroup\$Adam– Adam2011年12月30日 02:04:40 +00:00Commented Dec 30, 2011 at 2:04
-
\$\begingroup\$ every time one reminds me of good habit (here not ditching enum) i +1 :) \$\endgroup\$Vinayak Garg– Vinayak Garg2011年12月30日 06:26:48 +00:00Commented Dec 30, 2011 at 6:26