C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::sizeof_minus_trailing_padding

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 3 Dec 2023 17:35:37 +0000
On Fri, Dec 1, 2023 at 9:47 AM Thiago Macieira wrote:
>
> I am also skeptical that you're allowed to use the tail padding in your A
> class in the example. It isn't trivially copyable, but why wouldn't the
> members in that class be allowed to use the full sizeof(A) in this?


This is an interesting point. A person might write a POD struct as follows:

    class MyPlainOldData {
        long double a;
        char b;
        long long unsigned c;
        char d;
        void Reset(void) noexcept
        {
            std::memset(this,0,sizeof *this);
        }
    };

This would be problematic in the following:

    class Monkey {
        [[no_unique_address]] MyPlainOldData mypod;
        char c;
    };

because the 'Reset' method would wipe out the value of 'c'.

By the way, I only heard about no_unique_address for the first time
this past week . . . and I haven't been able to find any documentation
on the web about why it doesn't work with private data members. If I
change 'MyPlainOldData' from a class to a struct, Monkey's 'c' is no
longer stored in the preceding tail padding. What it is about private
data members that makes no_unique_address get turned off?

Received on 2023-12-03 17:35:48