Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-01

fgl.TFPGMap

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alex Shishkin

Following code causes error "Operator is not overloaded: "TObject" < "TObject""
How to make it work?

program generictest2;
{$mode objfpc}{$H+}
uses
   fgl;
operator < (a: TObject; b: TObject): boolean;
begin
   result := PtrInt(a) < PtrInt(b);
end;
type
   TMyMap = specialize TFPGMap <TObject, TObject>;
begin
end.

Reply by email

Honza
to Alex Shishkin

2011/1/20 Alex Shishkin :

Show quoted text 16 lines
Following code causes error "Operator is not overloaded: "TObject" < "TObject"" How to make it work? program generictest2; {$mode objfpc}{$H+} uses  fgl; operator < (a: TObject; b: TObject): boolean; begin  result := PtrInt(a) < PtrInt(b); end; type  TMyMap = specialize TFPGMap <TObject, TObject>; begin end.

AFAIK TFPGMap can be specialized only with types which have the relational operators predefined by the compiler. See also: http://bugs.freepascal.org/view.php?id=15480

You can take a look on this container lib, which might be helpful for your task: http://code.google.com/p/fprb/wiki/heContnrs

bflm freepascal-bits.blogspot.com

Reply by email

Alex Shishkin
to Honza

20.01.2011 22:18, Honza пишет:

Show quoted text 20 lines
2011/1/20 Alex Shishkin: > Following code causes error "Operator is not overloaded: "TObject"< > "TObject"" > How to make it work? > > program generictest2; > {$mode objfpc}{$H+} > uses > fgl; > operator< (a: TObject; b: TObject): boolean; > begin > result := PtrInt(a)< PtrInt(b); > end; > type > TMyMap = specialize TFPGMap<TObject, TObject>; > begin > end. AFAIK TFPGMap can be specialized only with types which have the relational operators predefined by the compiler. See also: http://bugs.freepascal.org/view.php?id=15480
As I understand problem is in the compiler. It cant use overloaded
operator if it was defined after generic.
BTW this code compiles successfully.
program generictest2;
{$mode objfpc}{$H+}
operator > (a: TObject; b: TObject): boolean;
begin
   result := PtrInt(a) > PtrInt(b);
end;
type
   generic TComparator <T> = class
     class function Compare(a,b:T): Integer;
   end;
class function TComparator.Compare(a, b: T): Integer;
begin
   if a>b then
     Result := 1
   else if b>a then
     Result := -1
   else
     Result :=0;
end;
type
   TObjectComparator = specialize TComparator<TObject>;
begin

end.
Data can be any type but Key only "native comparable". If drop TFPGMap.KeyCompare along with DataCompare leaving only custom comparison it will work with any Key type.

You can take a look on this container lib, which might be helpful for your task: http://code.google.com/p/fprb/wiki/heContnrs

Good lib, but Lazarus codetools does not support nested classes as for now.

Reply by email

Honza
to Alex Shishkin

2011/1/20 Alex Shishkin :

As I understand problem is in the compiler. It cant use overloaded operator if it was defined after generic.

AFAIK exactly that is the problem and that's why TFPGMap can't cope with some types.

Show quoted text 5 lines
> You can take a look on this container lib, which might be helpful for your > task: > http://code.google.com/p/fprb/wiki/heContnrs Good lib, but Lazarus codetools does not support  nested classes as for now.

That's unfortunately a thing I can't help you with.

bflm freepascal-bits.blogspot.com

Reply by email

leledumbo
to Alex Shishkin: 20.01.2011 22:18, Honza пишет: As I understand problem is in the compiler. It cant…

Another good one: http://code.google.com/p/stlpascal/ STLPascal Uses pattern commonly used by C++ STL, but fortunately written in a much more readable way. It has more data structures than heContnrs, but missing the BTree implementation. Really, IMHO somebody's got to merge all of these libraries (including fgl).

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/fgl-TFPGMap-tp3350031p3350384.html Sent from the Free Pascal - General mailing list .

Reply by email

Alex Shishkin
to leledumbo

21.01.2011 0:26, leledumbo пишет:

Show quoted text 5 lines
Another good one: http://code.google.com/p/stlpascal/ STLPascal Uses pattern commonly used by C++ STL, but fortunately written in a much more readable way. It has more data structures than heContnrs, but missing the BTree implementation. Really, IMHO somebody's got to merge all of these libraries (including fgl).

stlpascal released under GPLv3 but fpc libraries under modified LGPL. If stlpascal will change license to modified LGPL it will be good to see it in fcl.

Reply by email

Paul Ishenin
to Alex Shishkin: Following code causes error "Operator is not overloaded: "TObject" < "TObject"" How to…
Alex Shishkin wrote 16 lines
Following code causes error "Operator is not overloaded: "TObject" < "TObject"" How to make it work? program generictest2; {$mode objfpc}{$H+} uses fgl; operator < (a: TObject; b: TObject): boolean; begin result := PtrInt(a) < PtrInt(b); end; type TMyMap = specialize TFPGMap <TObject, TObject>; begin end.

Please report this bug to the tracker.

Best regards,
Paul Ishenin

Reply by email

Alex Shishkin
to Paul Ishenin

21.01.2011 3:07, Paul Ishenin пишет:

Show quoted text 26 lines
21.01.2011 0:28, Alex Shishkin wrote: > Following code causes error "Operator is not overloaded: "TObject" < > "TObject"" > How to make it work? > > program generictest2; > {$mode objfpc}{$H+} > uses > fgl; > operator < (a: TObject; b: TObject): boolean; > begin > result := PtrInt(a) < PtrInt(b); > end; > type > TMyMap = specialize TFPGMap <TObject, TObject>; > begin > end. Please report this bug to the tracker. Best regards, Paul Ishenin _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Also please see http://bugs.freepascal.org/view.php?id=18573, this related with generics to.
Alex

Reply by email