C++ Logo

std-discussion

Advanced search

Re: Possible defect in definition of standard-layout

From: Myria <myriachan_at_[hidden]>
Date: Tue, 9 May 2023 16:09:55 -0700
On Tue, May 9, 2023 at 15:18 Lénárd Szolnoki via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> Hi,
>
> Consider the following code:
>
> #include <type_traits>
>
> struct A {};
> struct B : A { char ch; };
>
> struct C : A {
> B b;
> };
>
> static_assert(std::is_standard_layout_v<C>);
> static_assert(sizeof(C) == 2);
>
> In my reading of https://eel.is/c++draft/class.prop#3 C is standard-
> layout. GCC with libstdc++ agrees.



> Is this a defect, or did I make a mistake somewhere in my reasoning?


Interesting… 3.7’s footnote is definitely the intended effect, but yeah,
it doesn’t look like 3.7 actually does what the footnote says in the case
you found.

Playing around, I noticed this:

struct X { };
struct Y {
    X x;
};
struct Z : X {
    Y y;
};

By 3.7, Z is definitely not standard-layout, but GCC thinks it is. So
GCC’s implementation of the rule is wrong. Clang and MSVC get Z correctly
labeled as non-standard-layout.

Melissa

>

Received on 2023-05-09 23:10:08