C++ Logo


Advanced search

Subject: Common-initial-sequence, read/write operations and subobjects
From: Andrey Davydov (andrey.davydov_at_[hidden])
Date: 2020-02-01 00:54:24

According to the standard the following code contains UB

union U {
    struct A {
        int x, y;
    } a;
    struct B {
        int s, t;
    } b;

int f() {
    U u;
    u.a = { 1, 2 };
    u.b.s = 3; // ends lifetime of u.a
    return u.b.t; // u.a.y is already dead, u.b.t is not initialized yet

U::A and U::B are layout-compatible and so subobjects of U could be read
interchangeably, but mix of read and write operations is still prohibited.
I don't see fundamental reasons for this restriction, for instance how it
prevents TBAA or something similar. Is the current behavior a deficiency of
the standard which should be fixed somehow in the future or am I missing
something important?

Andrey Davydov

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups