Shallow copy object when type is know

Tofu Ninja via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 20 06:01:10 PDT 2016


On Wednesday, 20 April 2016 at 12:32:48 UTC, Tofu Ninja wrote:
> Is there a way to shallow copy an object when the type is 
> known? I cant seem to figure out if there is a standard way. I 
> can't just implement a copy function for the class, I need a 
> generic solution.

I feel like

void shallow_copy(T)(T s, T d) if(is(T == class)) {
	ubyte[] sp = (cast(ubyte*)s)[0..T.classinfo.m_init.length];
	ubyte[] dp = (cast(ubyte*)d)[0..T.classinfo.m_init.length];
	dp[] = sp[];
}

would be really unsafe but I dont see any other way to do it... I 
dont know what kind of hidden members I might be messing up when 
doing that.


More information about the Digitalmars-d-learn mailing list