Sign in Join

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

Mailing lists / fpc-pascal / 2012-04

Can it map class type as key?

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

ZHANG Dao-yuan
Show quoted text 8 lines
program moi; {$mode objfpc} uses fgl; type tSI= specialize tFpGMap<integer, tObject>; // tIS= specialize tFpGMap<tObject, integer>; begin end.

Hi,
I'm trying to use template in fpc 2.6.0. I can map class type as value as above code can be compiled. But if I uncomment the declaration of tIS -- map class type as key, fpc fail to compile and print out these error msgs:

Error: Operator is not overloaded: "TObject" < "TObject" Error: Operator is not overloaded: "TObject" > "TObject" t.pas(10) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)

Any way to resolve it?

JC Chu
to ZHANG Dao-yuan

It fails because the operators < and > are not defined for TObject, which is assumed by TFPGMap<TKey, TValue>.KeyCompare().

As a makeshift you can implement a record type to wrap TObject and define these operators for it. See the attached file for an example.

Hope it helps.

ZHANG Dao-yuan wrote 26 lines
> program moi; > {$mode objfpc} > uses fgl; > type > tSI= specialize tFpGMap<integer, tObject>; > // tIS= specialize tFpGMap<tObject, integer>; > begin > end. Hi, I'm trying to use template in fpc 2.6.0. I can map class type as value as above code can be compiled. But if I uncomment the declaration of tIS -- map class type as key, fpc fail to compile and print out these error msgs: Error: Operator is not overloaded: "TObject" < "TObject" Error: Operator is not overloaded: "TObject" > "TObject" t.pas(10) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled) Any way to resolve it? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Best Regards, JC Chu
Lukasz Sokol
to JC Chu
JC Chu wrote 9 lines
It fails because the operators < and > are not defined for TObject, which is assumed by TFPGMap<TKey, TValue>.KeyCompare(). As a makeshift you can implement a record type to wrap TObject and define these operators for it. See the attached file for an example. Hope it helps.

The OP asks for generics usage, not for the operator overload (notice the 'specialize' keyword).

L.

JC Chu
to Lukasz Sokol

Notice the {$MODE DELPHI} directive.

Lukasz Sokol wrote 18 lines
On 26/04/2012 10:14, JC Chu wrote: > It fails because the operators < and > are not defined for TObject, > which is assumed by TFPGMap<TKey, TValue>.KeyCompare(). > > As a makeshift you can implement a record type to wrap TObject and > define these operators for it.  See the attached file for an > example. > > Hope it helps. > The OP asks for generics usage, not for the operator overload (notice the 'specialize' keyword). L. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Best Regards, JC Chu
ZHANG Dao-yuan
to JC Chu

Thanks for your makeshift :) . But the problem still confusing me. My code is in type-section and it's a declaration not a statement.

tFpGMap<tObject

The form of the code is similar to a comparision expression and `<' here looks like a less-than operator. But they are not. In my opinion, fpc should not take the declaration as a statement then try to search for the nonsense operator-overloaded methods.

JC Chu
to ZHANG Dao-yuan

I’m not quite getting what you’re trying to say… Free Pascal and Delphi do have distinct syntaxes for generic type definition and specialization. You may want to consult respective documentations to clarify your understanding of the code.

◦ Delphi ―
<http://docwiki.embarcadero.com/RADStudio/en/Overview_of_Generics> ◦ Free Pascal ―
<http://www.freepascal.org/docs-html/ref/refch8.html>

Hope it helps.

ZHANG Dao-yuan wrote 5 lines
> tFpGMap<tObject The form of the code is similar to a comparision expression and `<' here looks like a less-than operator. But they are not. In my opinion, fpc should not take the declaration as a statement then try to search for the nonsense operator-overloaded methods.
Best Regards, JC Chu
Sven Barth
to ZHANG Dao-yuan: I'm trying to use template in fpc 2.6.0. I can map class type as value as above code can…
"ZHANG Dao-yuan" wrote 13 lines
> > program moi; > {$mode objfpc} > uses fgl; > type > tSI= specialize tFpGMap<integer, tObject>; > // tIS= specialize tFpGMap<tObject, integer>; > begin > end. Hi, I'm trying to use template in fpc 2.6.0. I can map class type as value as

above code can be compiled. But if I uncomment the declaration of tIS -- map class type as key, fpc fail to compile and print out these error msgs:

Show quoted text 6 lines
Error: Operator is not overloaded: "TObject" < "TObject" Error: Operator is not overloaded: "TObject" > "TObject" t.pas(10) Fatal: There were 2 errors compiling module, stopping Fatal: Compilation aborted Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not

specify a source file to be compiled)

Any way to resolve it?

This has nothing to do with < and > in the declaration. TFPGMap uses < and > comparisons for key comparisons, so these operators need to be overloaded. You can't use global operators here, because of scope problems, so you need to use either a different type or wrap them in a record with operators.

Regards,
Sven

Honza
to ZHANG Dao-yuan: Thanks for your makeshift :) . But the problem still confusing me. My code is in…

2012/4/26 ZHANG Dao-yuan :

Show quoted text 9 lines
Thanks for your makeshift :) . But the problem still confusing me. My code is in type-section and it's a declaration not a statement. > tFpGMap<tObject The form of the code is similar to a comparision expression and `<' here looks like a less-than operator. But they are not. In my opinion, fpc should not take the declaration as a statement then try to search for the nonsense operator-overloaded methods.

You might want to have a look at
http://code.google.com/p/fprb/wiki/TheCmpBTreeMap

-jan

ZHANG Dao-yuan
to JC Chu: I’m not quite getting what you’re trying to say… Free Pascal and Delphi do have…

Yes, I didn't clearly understand the generic mechanism in fpc/tfpgmap. I've read, `A class can be seen as a pointer to an object, or a pointer to a record, with methods associated with it.' in fpc reference.pdf, chapter 6 - Classes. Hence I think fpc takes object as pointer when specialize tfpgmap template.

Now I catch your idea. The specialization of tfpgmap is similar to std::map in C++ (must define a (bool operator<(ref to another obj)const) function for the KEY class). I'll read the source code of unit fgl to deepen understanding.

Thank you very much.

JC Chu wrote 11 lines
I’m not quite getting what you’re trying to say… Free Pascal and Delphi do have distinct syntaxes for generic type definition and specialization. You may want to consult respective documentations to clarify your understanding of the code. ◦ Delphi ― <http://docwiki.embarcadero.com/RADStudio/en/Overview_of_Generics> ◦ Free Pascal ― <http://www.freepascal.org/docs-html/ref/refch8.html> Hope it helps.
ZHANG Dao-yuan
to Sven Barth: above code can be compiled. But if I uncomment the declaration of tIS -- map class type…
Sven Barth wrote 5 lines
This has nothing to do with < and > in the declaration. TFPGMap uses < and > comparisons for key comparisons, so these operators need to be overloaded. You can't use global operators here, because of scope problems, so you need to use either a different type or wrap them in a record with operators. Regards, Sven

I have another question now. Why doesn't the developers of fgl overload `>' `<' operator for class-type in fgl unit? ie, put

operator >(a, b: tObject) r: boolean; operator <(a, b: tObject) r: boolean;

into the interface section
and put

Show quoted text 8 lines
operator >(a, b: tObject) r: boolean; begin r:= ptrUInt(a) > ptrUInt(b); end; operator <(a, b: tObject) r: boolean; begin r:= ptrUInt(a) < ptrUInt(b); end;

into the implementation section.

This seems a easy way to resolve the scope problem.

ZHANG Dao-yuan
to ZHANG Dao-yuan
ZHANG Dao-yuan wrote
I have another question now. Why doesn't the developers of fgl overload `>' `<' operator for class-type in fgl unit?

Ah, it will override overloaded-class-(lessThan|greaterThan)-operator which is defined in other units. :( By the way, Will `class operator' within class be allowed to define in the next major release?