Proposal: Operator overloading without temporaries

Don Clugston dac at nospam.com.au
Mon Apr 3 00:10:04 PDT 2006


Bruno Medeiros wrote:
> Don Clugston wrote:
>> Background: Operator overloading, in the form it exists in C++ and 
>> currently in D, inherently results in sub-optimal code, because it 
>> always results in unnecessary temporary objects being created.
>>
>> For example,
>> X = A - ((B*C) + D)* E;
>>
>> becomes:
>> T1 = B * C;
>> T2 = T1 + D;
>> T3 = T2 * E;
>> T4 = A - T3;
>> X = T4;
>> Four objects were created, whereas only one was strictly required.
> 
> Ok, I'm new to this, so it took me a while to understand the problem. 
> Let's see if I got it right: this is actually only a problem when the 
> operator methods explicitly instantiate a *class object*, to be used as 
> the return of the method, right?

Not really, it applies everywhere that you can have overloaded 
operators. The cost of a temporary will be greater with classes, but for 
structs, eliminating temporaries will make it much easier for the 
compiler to optimise.




More information about the Digitalmars-d mailing list