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

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Sat Jul 30 20:27:27 CEST 2011


if i understand correctly, your problem is not operator overloading or
whatever in the language.

You have references.

Now you want to write counted references to references and deal with
this in the overloaded operators

your problem is right into the counted reference to reference.

A := B makes A point to the same reference as B, and this is perfectly
correct semantic, but this will not work correctly by itself  if the
referenced object is itself a reference (without counting) to another
thing.

a pointer to a pointer...

you have to refcount the original pointer instead.

So the solution is to have a singleton that holds all references to
all bignums and then work from that on.

2011/7/30 Bernd <prof7bit at googlemail.com>:
> 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.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



More information about the fpc-pascal mailing list