Structs, Speed and refs.

Daniel Kozák" <kozzi11 at gmail.com> Daniel Kozák" <kozzi11 at gmail.com>
Tue Oct 23 03:36:10 PDT 2012


Hi,

First of all, I think there is no difference in speed between

LargeStruct foo, too, temp;
temp = foo - too;
bar.func(temp);

and with func without ref.

How looks your opBinary(Right) method?

For eg. this works for me:

struct LargeStruct {
	int x;
	
	auto ref opBinary(string op)(LargeStruct rhs) if (op == "-") {		
		this.x = this.x - rhs.x;
		return this;
	}
}

class Bar {
   void func(ref LargeStruct st) {}
}


void main(string[] args)
{
	Bar bar = new Bar();
	LargeStruct foo, too;
	bar.func(foo - too);
}

However this will modify foo struct.


On Tuesday, 23 October 2012 at 09:44:06 UTC, Jakob Bornecrantz 
wrote:
> Hey everybody!
>
> A bit of background; I'm porting some code over from D1 to D2 
> and I ran into a bit of a problem. I have a bunch of code that 
> looks something like this:
>
> class Bar : Other {
>   override void func(ref LargeStruct st) {}
> }
>
> Bar bar;
> LargeStruct foo, too;
> bar.func(foo - too);
>
> This compiles just fine in D1 but no longer in D2. Because the 
> temp created by "foo - too" is not a lvalue apparently. I don't 
> want to remove the "ref" storage type because i still want it 
> to be fast. And I rather have neater looking code vs:
>
> LargeStruct foo, too, temp;
> temp = foo - too;
> bar.func(temp);
>
> One solution is to turn "in" structs into pass by reference, eg 
> const ref, but not require lvalue semantics at the call site?
>
> Cheers, Jakob.




More information about the Digitalmars-d mailing list