Hello all,
This needs some initial explanation, so pls bear with me.
Some years ago I did some experiments with 'standard' operator overloading (oo) under iirc FPC 3.0.4. 'Standard' in this case means having a separate structure and implementing oo like e.g. (from my 128 bit integer unit)
operator +( constref O1: tUInt128; constref O2: tUInt128 ) Sum: tUInt128;
At that time I discovered that I had to be carefull when implementing the oo, as the compiler directly mapped the result type ('Sum' in above) to the left hand side of an expression. This could lead to incorrect results, if not implemented carefully, in expressions like e.g.
A := A + B; // A, B of type tUInt128
I verified this, at the time, by looking at the compiler generated asm - even including some notes about it in my sources.
Recently I have reverted to that topic (under FPC 3.2.2) and took my old Int128 unit up again to implement some neglected topics and also started, due to a hint from Bart, some experiments with operator overloading in advanced records, like e.g.
class operator :=( const X: UInt8 ):tSpecial;
I made the advanced record a managed object by implementing 'Initialize' & 'Finalize' methods.
In both cases it now looks like the compiler is implementing a hidden, intermediate result variable, from which the result is copied back to the left hand side of an expression. I detected that as I saw more calls to 'Initialize' & 'Finalize' then I would have expected from used variables alone (on the calling side).
Now I have some questions re the above
- am I correct that something has changed re oo between FPC 3.0.4 and 3.2.2?
- am I correct regarding that hidden variable?
- if the above is correct, does that force me to also implement 'Copy' if I made an advanced record a managed object via 'Initialize' & 'Finalize'? Especially if the record contains a pointer to data on the heap, like
private
A: UInt32;
B: UInt32;
C: UInt32;
D: pUInt64;
public
Kind regards,
MathMan