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

后端 未结 6 1438
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: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?

提交回复
热议问题