Taking the address of an rvalue struct

Cristi Cobzarenco cristi.cobzarenco at gmail.com
Fri Jul 22 08:36:20 PDT 2011


This is actually a question that has arisen in my GSoC problem, thanks
for asking David. In this case I'm pretty sure it would break if one
allocated something else on the stack. The case I'm more interested in
is something like this:

struct Sum {
   Value *a, *b;

   int eval() { return *a + *b; }
}

struct Value {
    int v;
    Value* addressOf() @property { return &this; }
}

void main() {
    writeln( Sum( Value(10).addressOf, Value(20).addressOf ).eval() );
}

To me it seems that as long as all the literals are allocated in the
same expression this should work, right?
}

---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco



On 22 July 2011 17:22, dsimcha <dsimcha at yahoo.com> 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);
> }
>


More information about the Digitalmars-d mailing list