开发者

Neither of undefined, implementation defined, unspecifed behavior

C++03

$5.3.3/2 - "The size of a most derived class shall be greater than zero (1.8)."

$1.8/4 - "Unless it is a bit-field (9.6), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage."

My question then is:

Is the size of an empty class unspecified, implemention defined? Is it supposed开发者_运维知识库 to be documented by the compiler documentation? Those two quotes leave it so open ended as far as my understanding is correct.


It's unspecified (except that it must be greater than 0). It's not implementation defined, either (so it doesn't need to be documented).

I'm not sure why an implementation would use any size other than 1 for an empty class (assuming by "empty class" we're talking about a class that doesn't even derive from another class), but I suppose it could.


To address Chubsdad's question about what determines that this is unspecified behavior (as opposed to implementation defined):

The standard defines "unspecified behavior" as:

behavior, for a well-formed program construct and correct data, that depends on the implementation. The implementation is not required to document which behavior occurs.

It defines "implementation-defined behavior" as:

behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation shall document

So the only difference between the two is that implementation-defined behavior must be documented. The standard will say when behavior must be documented (usually by saying that a behavior is implementation-defined)

Unfortunately, the standard doesn't always directly state that a behavior is unspecified (or undefined). So, some analysis is in order:

Since the standard says:

  • that sizeof must evaluate to a greater-than-zero result for an empty class,
  • doesn't say what that value must be (other than greater-than-zero)
  • doesn't say that the value must be documented (or that it's implementation-defined)

by a process of elimination, the sizeof an empty class is unspecified.

The possible problem with this analysis (and with much analysis of something as complex as the C++ standard) is that there's the possibility that there's some other corner of the standard that I've missed that might require the sizeof an empty class be some specific value. And that requirement might be by inference (or would it be deduction?) from some other rules; it might not be stated outright. It's not always easy to track down all the areas of the standard that might apply to a particular issue.

If that's the case, then someone will hopefully note that and shoot my argument down.


Most derived type means the class of a complete object of a class type (1.8 of the C++ standard (intro.object)). So an empty class that is instantiated must have a unique address, which implies that sizeof(empty class)>0. However, this also means that you can have a base class of zero size (also in 1.8 of the C++ standard).

So if you have to instantiate an empty class, it cannot have a size of zero. If it is a base class subobject, then it can have zero size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜