Nephi Immortal<immortalne...@gmail.com> writes:
enum has four constants and require additional 16 bytes.
Usually not true. In typical implementations of C++ (and C),
if you compile
enum { zero, one, two, three };
int f(const int p[])
{
return p[two] - p[three];
}
the generated code and data are the same as if you had compiled
int f(const int p[])
{
return p[2] - p[3];
}
i.e. the enum type does not take up space in the executable
program. It does take up space in debug information, symbol
tables, and such; but if you are compiling for a device with so
little memory that you need to worry about 16 bytes, then you
probably will not install those things into the device anyway.
Do you have idea if there is another way how to overcome union to
accept const instead of static const?
If the union does not need to be anonymous, you can do this:
static const union
{
char str[ 5 ];
struct Letter
{
char a;
char b;
char c;
char d;
} letter;
} u = {"ABCD"};
int main()
{
char A = u.letter.a; // read 'A' from str and put it in A variable
char B = u.letter.b; // read 'B' from str and put it in B variable
char C = u.letter.c; // read 'C' from str and put it in C variable
char D = u.letter.d; // read 'D' from str and put it in D variable
}
ability to store something, BUT NOT the value of it. The 2003 Standard
never write one part and read another. That actually has undefined
behavior. I don't know whether that was relaxed in C++11.