Can there be a C++ type that takes 0 bytes

后端 未结 6 1439
Happy的楠姐
Happy的楠姐 2021-01-02 14:21

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

相关标签:
6条回答
  • 2021-01-02 15:05

    The C++ standard demands explicitly that every type have size at least 1. This is intimately tied to the requirement that each object have a unique address (consider Foo x[10] if Foo had size zero).

    0 讨论(0)
  • 2021-01-02 15:07

    The standard explicitly prohibits the existence of an instance of a type with size 0, the reason is that if an object could have size 0, then two different objects could be located at the exact same address. An empty struct, for example, will be forced to have size > 0 to comply with that requirement even if when used as base of a different type, the compiler can have it have size == 0.

    What is it that you want to do with an empty class?

    0 讨论(0)
  • 2021-01-02 15:13

    It sounds like you want std::optional.
    It won't have a sizeof 0, but that's not important for expressing an empty value.


    On a related note, there is C++ proposal (P0146R1) to make void a regular type.
    The paper goes on to discuss why even sizeof(void) can't be 0.

    Why Isn't sizeof(void) Equal to 0?

    One suggestion that has repeatedly come up is to have sizeof(void) report 0 and to allow multiple instances to share the same address. This would prevent users from having to use tricks akin to the empty base optimization in order to make more optimal usage of memory. Ideally, this would be the case, however such a change to the language is both vast and out of scope of the proposal. Allowing a type to have a 0 size and to allow separate instances to share an address implies drastic and subtle breaking changes to existing code. For instance, if you were to make an array of such a void type, a pointer, at least in the traditional sense, would no longer be able to be used as an iterator into that array (notably meaning that generic code which relies on this would now fail for such a size 0 type). As well, any code that relies on an object's type and address as unique would fail for void types, even though it is otherwise perfectly acceptable. Finally, if such a size were permitted for void, it should really be allowed for any type, including user-defined types. Having a special rule for void would make one more thing to have to think about and deal with differently for void types. Instead, this proposal opts to leave the size of void unspecified and thereby governed by existing language rules. In practice, it is expected that void will likely be size 1 in most implementations, though this is not required. If an eventual change were made to the language to allow for size 0 types, then void would be able to implicitly take advantage of that.


    Although this question is targeted to C++, it should be noted that an empty struct in C may result in a sizeof of 0. This however, is undefined behavior.

    If the struct-declaration-list contains no named members, the behavior is undefined.

    0 讨论(0)
  • 2021-01-02 15:17

    You cannot instantiate any data type in C++ that takes up zero bytes. The Standard dictates than an empty class, such as:

    class Empty {};
    

    ...will result in the following being true:

    Empty emp;
    assert( sizeof(emp) != 0 );
    

    The reason for this is so that you can take the address of the object.

    EDIT: I originally said the sizeof would be 1, but per @Als' comment, I have found the relevant passage in the Standard, and it is indeed simply non-zero:

    [Classes] §9/3

    Complete objects and member subobjects of class type shall have nonzero size

    0 讨论(0)
  • 2021-01-02 15:18

    The typedef is misspelled:

    typedef int nullType[0];
    

    As others have pointed out, you cannot have an object of size 0; However, compilers can (and frequently do) provide the Empty Base Class optimization.

    • http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Empty_Base_Optimization
    • http://www.cantrip.org/emptyopt.html ("empty member" optimization)
    0 讨论(0)
  • 2021-01-02 15:18

    A variable in C++ can never take zero bytes. Every object must have unique address, which is not possible if the size is zero.

    By the way,int[0] is illegal in Standard C++. If you're using GCC, compile it with -pedantic option, you will get this warning:

    warning: ISO C++ forbids zero-size array 'x' [-pedantic]
    

    Also, the syntax for typedef should be this:

      typedef int array[100]; //zero cannot be size - illegal!
    
    0 讨论(0)
提交回复
热议问题