[fpc-pascal] Re: operator overloading and counting references / creating / destoying anonymous instances

Bernd prof7bit at googlemail.com
Sat Jul 30 19:27:14 CEST 2011


2011/7/30 Florian Klaempfl <florian at freepascal.org>:

> The automatic constructor/destructor concept of C++ causes the same
> overhead. And the overhead for a function or operator overloading based
> approach is the same imo. Or do you have any example where a function
> based approach performs better? Overloaded operators are converted into
> function calls, there is no real difference.

I didn't intend to mean it is better solved in C++ (or any other
language), I didn't want to specifically criticize the Operator
overloading how it is implemented in Object Pascal, I just spent some
time experimenting with trying to wrap this bignum library in some
clever way to make it look and behave nicely. I posted this thread
only because at some point I realized that whatever I do to make it
behave like a built in type it becomes slow and ineffective and the
wrapper becomes big and complicated.

The reason is because it IS a function call.

A := B + C;

A := add(B, C);

This will create a new A and free the old one. I have no idea how to
prevent this.

add(A, B, C);

with var or out arguments would enable me to re-use the existing A.

If I cannot find a way to let me look to the left *trough* the :=
operator from inside the + operator I have no Idea how to implement
something like this, so I always have to create a new instance of A
and let the old one be freed.

The solution that I have chosen now will force me to explicitly create
and free them manually and all my methods take all variables as
arguments, have side effects and don't return anything. The only
operators that I have overridden are the comparison operators.

Here is the unit I am talking about in its current form (this is at
least the fifth different variation/rewrite of it already and likely
to change yet again once I have a new idea):
http://code.google.com/p/fpbitcoin/source/browse/trunk/openssl_bignum.pas

and here is an example where it is used (currently the only place
because it is all still far from complete):
http://code.google.com/p/fpbitcoin/source/browse/trunk/bitcoin_base58.pas

And since I believe this is a very deep and fundamental problem and
not only related or limited to FPC and not easily solved without
massive changes to the compiler and the entire operator overloading
syntax (if it is possible at all) I did not intend to make this a
Pascal bashing thread. I LOVE this language and this compiler and the
related projects around it.



More information about the fpc-pascal mailing list