I’m trying to declare a C++ variable that takes up zero bytes. Its in a union, and I started with the type as int[0]. I don’t know if that is actually zero bytes (although sizeof(int[0]) was 0). I need a better way to declare a 0 byte type, and hopefully one that can be typedefed to something like nullType or emptyType. The variable is in a union, so in the end memory is reserved anyway. I tried void on the off chance it would work, but C++ complained. I’m using Ubuntu 10.10, with a current version of the kernel, and an up-to-date GCC.
Here’s the union:
union RandomArgumentTypesFirst
{
uint uintVal;
nullType nullVal;
}
And here is the typedef:
typedef int[0] nullType;
The compiler says this about the typedef:
error: variable or field ‘nullVal’ declared voidmake[2]:
When I typed in int[0], it worked. Any suggestions?
EDIT:
As @fefe said in the comments, the int[0] may be provided as an extension by the compiler. GCC’s website says that the compiler has many extensions by default.
The typedef is misspelled:
As others have pointed out, you cannot have an object of size 0; However, compilers can (and frequently do) provide the
Empty Base Classoptimization.