The following seems to work, but feels like luck. When foo returns rc should be taken off the stack. If I recall, in C++ something like this would crash, but why not here?

import std.stdio;
struct RC {
  this(this) { data = data.dup; }
  int[] data;
}
struct T {
  const(RC) *rc;
  void goo() {
    writeln("Data is ", rc.data);
  }
}

T foo() {
  RC rc = { [1,2,3] };
  return T(&rc);
}

void main() {
  T t = foo();
  t.goo();
}

I'm trying to get around issues with

struct T {
   const(S) s;
}

by avoiding the postblit and having const(S) *s. I just want to know that it is always safe and if it is how?

Thanks
Dan


Reply via email to