www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Copy only frame pointer between objects of nested struct

reply "Peter Alexander" <peter.alexander.au gmail.com> writes:
Consider:

auto foo(T)(T a) {
	T b;  // Error: cannot access frame pointer of main.X
	b.data[] = 1;
	return b;
}

void main() {
	struct X {
		this(int) {}
		int[4096] data;
	}
	foo(X());	
}

Note the error is because you cannot construct the main.X object 
without a frame pointer.

You could do `T b = a` here to get a's frame pointer, but it 
would also copy all of a's data, which is expensive and 
unnecessary.

Is there a way to only copy a's frame pointer into b?

(Note: this is just an illustrative example, real problem here: 
https://issues.dlang.org/show_bug.cgi?id=13935)
Jan 06 2015
parent reply Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 01/06/15 23:14, Peter Alexander via Digitalmars-d-learn wrote:
 auto foo(T)(T a) {
     T b;  // Error: cannot access frame pointer of main.X
     b.data[] = 1;
     return b;
 }
 
 void main() {
     struct X {
         this(int) {}
         int[4096] data;
     }
     foo(X());   
 }
 
 Note the error is because you cannot construct the main.X object without a
frame pointer.
 
 You could do `T b = a` here to get a's frame pointer, but it would also copy
all of a's data, which is expensive and unnecessary.
 
 Is there a way to only copy a's frame pointer into b?
The obvious hack would be T b = void; b.tupleof[$-1] = a.tupleof[$-1]; but you probably don't want to do it like that...
 (Note: this is just an illustrative example, real problem here:
https://issues.dlang.org/show_bug.cgi?id=13935)
That shows a static struct, so I'm not sure it's the same problem. artur
Jan 06 2015
parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Tuesday, 6 January 2015 at 23:32:25 UTC, Artur Skawina via
 That shows a static struct, so I'm not sure it's the same 
 problem.
static structs with template alias parameters to local symbols count as nested structs. Your solution would likely work, but yes, I'm looking for something less hacky :-)
Jan 07 2015