C++ Logo

std-proposals

Advanced search

Re: Explicit Order

From: Brian Bi <bbi5291_at_[hidden]>
Date: Thu, 27 Feb 2020 10:49:59 -0600
On Thu, Feb 27, 2020 at 10:37 AM Ryan Nicholl via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Imagine for a moment you have a structure,
>
> struct foo
> {
> bar a;
> baz b;
> bing c;
> };
>
> Now, what if we want to change the order of initialization of the members,
> we have to reorder them. This might cause problems if the order "a b c" in
> the struct is optimal for performance cache wise, but the members must
> actually be initialized in the order "c a b".
>
> Currently this must be done with placement new... not terse!
>
> I propose the following, in any class or struct, allow "explicit order {
> ... };" declaration. Where "order" is a contextual keyword.
>
> E.g.
>
>
> struct foo
> {
> bar a;
> baz b;
> bing c;
>
> explicit order { c, a, b };
> };
>
> Now all constructors/destructors will run in the order of construction, c,
> a, then b, but the binary ABI layout is still a b c.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

I think it's better to have the order of construction and destruction go
according to the declaration order, and add some new feature to control the
memory layout, rather than the reverse.

The reason why I say this is that order of construction and destruction are
far more likely to affect correctness of the program, than order of
allocation of the members. e.g., we can have

struct foo {
    bar a;
    baz b {&a}; // baz constructor might dereference this pointer
};

If there's an `explicit order` block all the way at the bottom, this can
break. Theoretically you may have some code that relies on the memory
addresses being in a particular order, but I think that's much rarer.

-- 
*Brian Bi*

Received on 2020-02-27 10:52:53