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

后端 未结 6 1455
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: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!
    

提交回复
热议问题