Before C was standardized, many compilers would not have to work with null types if the code never tried to subtract one pointer to zero size from another. These types were useful, and supporting them was easier and cheaper than forbidding them. Other compilers decided to ban such types, however, and some static assertion code could rely on the fact that they would scream if the code tried to create an array of zero size. The authors of the Standard are faced with a choice:
Allow compilers to silently accept zero-size array declarations, even in cases where the purpose of such statements would be to initiate diagnostics and interrupts, and require all compilers to accept such declarations (although not necessarily silently).
Allow compilers to silently accept declarations of arrays of zero size, even in cases where the purpose of such statements would be to initiate diagnostics and interruptions, and also allow compilers to meet with such declarations in order to either stop compilation or continue at their leisure.
Require implementations to issue diagnostics if the code declares a zero-sized array, but then allow implementations to either interrupt compilation or continuation (with any semantics that they consider necessary) at their leisure.
The authors of the Standard have chosen No. 3. Therefore, declarations of zero-size arrays are considered the standard βextensionβ, although such constructions were widely supported before the standard forbade them.
The C ++ Standard allows empty objects to exist, but in order to allow the addresses of empty objects to be used as tokens, it requires that they have a minimum size of 1. For an object that has no members, therefore size 0 will violate the Standard. However, if an object contains elements with a zero size, the C ++ standard does not impose requirements on how it is processed, except that a program containing such an announcement must initiate diagnostics. Since most codes that use such declarations expect the resulting objects to be zero size, the most useful behavior for compilers receiving such code is to process them this way.
supercat Nov 17 '17 at 18:18 2017-11-17 18:18
source share