2

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.

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Class and struct layouts are typically padded to the machine word size, so a size of 4 seems more likely on typical architectures.
How do we reach that conclusion is my question? It is soooo open ended to my mind
It's specified to be non-zero. :)
@karl: that seems reasonable. I think it's been 1 when I've examined the size of an empty class, but it's been a long time and probably only on one compiler, so small sample size might skew my results. I might even be remembering wrong...
So if it is specified to be non zero, then it can't be unspecified behavior, meaning it is implementation defined. Then it should be documented
|
1

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.

1 Comment

That case of 0 is called a subobject in Standard parlance IMO. My doubt is about the most derived object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.