June 09, 2019
On 6/9/2019 1:05 AM, Manu wrote:
> I am really really tired of this pattern:

Is your real complaint this:
---
  #include <stdio.h>

  struct B { };
  struct S : B { char c; };
  struct T { B b; char c; };

  void main(){
    printf("%d %d\n", sizeof(struct S), sizeof(struct T));
  }
---
which prints:

  1 2

?
June 10, 2019
On Monday, 10 June 2019 at 04:03:38 UTC, Nicholas Wilson wrote:
> There is a (proposal?) for a no unique address attribute to disappear the address of structs the take up no room.

Found it:

https://en.cppreference.com/w/cpp/language/attributes/no_unique_address

Looks like there are a bunch of weird edge cases with it though:

struct Empty {};

struct Z {
    char c;
    [[no_unique_address]] Empty e1, e2;
};

    // e1 and e2 cannot share the same address because they have the
    // same type, even though they are marked with [[no_unique_address]].
    // However, either may share address with c.
    static_assert(sizeof(Z) >= 2);

June 09, 2019
On Sun, Jun 9, 2019 at 9:50 PM Walter Bright via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>
> On 6/9/2019 1:05 AM, Manu wrote:
> > I am really really tired of this pattern:
>
> Is your real complaint this:
> ---
>    #include <stdio.h>
>
>    struct B { };
>    struct S : B { char c; };
>    struct T { B b; char c; };
>
>    void main(){
>      printf("%d %d\n", sizeof(struct S), sizeof(struct T));
>    }
> ---
> which prints:
>
>    1 2
>
> ?

That is indeed the feature that necessitates my 'solution', but `alias
this` is generally undesirable in this pattern in its own right, for
some of the reasons I listed.
`alias this` does not feel satisfying in this context in its own
right. Also, if this hack takes the alias this slot, then it can't be
used for what it's usually used for (enabling implicit conversion) and
sometimes needs conflict.
June 10, 2019
On 6/9/2019 10:13 PM, Nicholas Wilson wrote:
> On Monday, 10 June 2019 at 04:03:38 UTC, Nicholas Wilson wrote:
>> There is a (proposal?) for a no unique address attribute to disappear the address of structs the take up no room.
> 
> Found it:
> 
> https://en.cppreference.com/w/cpp/language/attributes/no_unique_address
> 
> Looks like there are a bunch of weird edge cases with it though:
> 
> struct Empty {};
> 
> struct Z {
>      char c;
>      [[no_unique_address]] Empty e1, e2;
> };
> 
>      // e1 and e2 cannot share the same address because they have the
>      // same type, even though they are marked with [[no_unique_address]].
>      // However, either may share address with c.
>      static_assert(sizeof(Z) >= 2);
> 

I didn't know about that attribute. Thanks for digging it up. Sheesh.
June 10, 2019
On 6/9/2019 10:52 PM, Manu wrote:
> On Sun, Jun 9, 2019 at 9:50 PM Walter Bright via Digitalmars-d
> <digitalmars-d@puremagic.com> wrote:
>>
>> On 6/9/2019 1:05 AM, Manu wrote:
>>> I am really really tired of this pattern:
>>
>> Is your real complaint this:
>> ---
>>     #include <stdio.h>
>>
>>     struct B { };
>>     struct S : B { char c; };
>>     struct T { B b; char c; };
>>
>>     void main(){
>>       printf("%d %d\n", sizeof(struct S), sizeof(struct T));
>>     }
>> ---
>> which prints:
>>
>>     1 2
>>
>> ?
> 
> That is indeed the feature that necessitates my 'solution', but `alias
> this` is generally undesirable in this pattern in its own right, for
> some of the reasons I listed.

I don't see where you listed reasons? Other than it just being unsightly?

> `alias this` does not feel satisfying in this context in its own
> right. Also, if this hack takes the alias this slot, then it can't be
> used for what it's usually used for (enabling implicit conversion) and
> sometimes needs conflict.

Are you using 'alias this' to try to do multiple inheritance? Please don't :-(

June 10, 2019
On Monday, 10 June 2019 at 09:35:25 UTC, Walter Bright wrote:
> I didn't know about that attribute. Thanks for digging it up.

Np.

> Sheesh.

Touché.

June 10, 2019
On 6/9/2019 10:52 PM, Manu wrote:
> That is indeed the feature that necessitates my 'solution'

One possibility is that if the alias this is referring to a struct with no fields, it gets a 0 size. This will mimic the behavior of C++.
June 10, 2019
On Monday, 10 June 2019 at 03:08:26 UTC, Walter Bright wrote:
> There are some rules in C++ where sometimes a struct with no fields occupies 1 byte, sometimes 0 bytes. I don't recall what they are at the moment.

Yes, empty base class optimization:

https://en.cppreference.com/w/cpp/language/ebo

June 10, 2019
On Sunday, 9 June 2019 at 08:05:34 UTC, Manu wrote:
> Imagine if we could just write:
>
> struct DerivedStruct : BaseStruct
> {
>     // derived members
>     //...
> }
>
> Just imagine!

+1

I dream of being able to do struct DerivedStruct: SomeInterface, but what you are asking is also something I was wishing for years...
June 10, 2019
On Monday, 10 June 2019 at 03:08:26 UTC, Walter Bright wrote:
> On 6/9/2019 12:40 PM, Vladimir Panteleev wrote:
>> Structs are always at least 1 byte in size, even when they contain no fields. This causes unnecessary bloat for reasons that I never understood.
>
> The reason is so that each struct instance is at a unique address.

What advantages does this provide? Surely it can't be more important than being able to place behavior/policy in structs with no overhead?