Taking the address of an rvalue struct

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Jul 22 09:13:53 PDT 2011


On 7/22/11 10:22 AM, dsimcha wrote:
> The following code uses the this reference in a struct member function to take
> the address of an rvalue struct.  Is it well-defined behavior equivalent to
> taking the address of a named stack-allocated struct, or is the compiler free
> to break it in optimized mode?
>
> struct SomeStruct {
>      int num;
>
>      this(int x) {
>         num = x;
>      }
>
>      SomeStruct* addressOf() @property {
>          return&this;
>      }
> }
>
> void main() {
>     auto ptr = SomeStruct(42).addressOf;
>     writeln(ptr.num);
> }

This has long been a feature that I attempted to remove several times. 
It steps from the fact that you can invoke methods on rvalue structs. 
Disallowing that would solve the issue, but would also eliminate a lot 
of convenience.


Andrei


More information about the Digitalmars-d mailing list