Skip to content

CWG2609 [expr.sizeof] The result of applying sizeof to empty class is standard? #89

Closed
cplusplus/draft
#5560
@xmh0511

Description

@xmh0511

Full name of submitter (unless configured in github; will be published with the issue): Jim X

[expr.sizeof] p1 says

The result of sizeof applied to any of the narrow character types is 1. The result of sizeof applied to any other fundamental type ([basic.fundamental]) is implementation-defined.

This rule means the result of sizeof to a non-narrow-character fundamental type is implementation-defined. The standard says nothing about whether the result of sizeof to a class type is implementation-defined or not, it just says

When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array.

It can only infer that sizeof to a class type that contains a subobject of a non-narrow-character fundamental type is also implementation-defined, as per [expr.sizeof] p1. This raises a issue, consider that

struct S{};
sizeof(S); // all implementations say the result is 1

[intro.object] p8 only says

Unless it is a bit-field, an object with nonzero size shall occupy one or more bytes of storage, including every byte that is occupied in full or in part by any of its subobjects.

It didn't specify a concrete number, merely the number is at least one. Such a class type does not contain any subobjects of fundamental types, as aforementioned, the result of sizeof to such a type is not clear whether it is guaranteed by standard or specified with implementation-defined.

All consistent behavior of implementations as if to say the result should be guaranteed by the standard but it was omitted in the standard.

Suggested resolution

we may need to clearly say that the result of sizeof applied to any type is implementation-defined except for narrow character types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions