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

后端 未结 6 1481
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

    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)

提交回复
热议问题