Skip to content

Common initial sequence is not respected when alignas is used #186040

@Halalaluyafail3

Description

@Halalaluyafail3

The following program demonstrates the issue:

#include<iostream>
#include<type_traits>
int main(){
    struct A{char x;};
    struct alignas(2)B{char x;};
    struct C{A y;char z;};
    struct D{B y;char z;};
    union U{C a;D b;};
    static_assert(std::is_standard_layout_v<C>);
    static_assert(std::is_standard_layout_v<D>);
    static_assert(std::is_layout_compatible_v<C,D>);
    U u;
    u.a.z=1;
    std::cout<<+u.b.z<<'\n';
}

Clang recognizes that C and D are both layout-compatible standard layout classes, therefore all of their members should be part of the common initial sequence. However, Clang does not seem to lay out their respective z members so that they have the same offset. There seems to be two possible solutions here. First, reject that C and D are layout-compatible and possibly also further reject that A and B are layout-compatible. Second, whenever a standard layout class is used as a member it must be aligned to the maximum possible alignment so that any alignment differences do not matter.

See also cplusplus/draft#8787.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:edgDoes the clang frontend diverge from edg compiler on this issuediverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions