struct opCmp?
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Thu May 14 15:07:17 PDT 2009
Nick Sabalausky wrote:
> Before I go to post a bug report I want to make sure I'm not missing
> something:
>
> On http://www.digitalmars.com/d/1.0/struct.html it says that structs support
> operator overloading. But I can't seem get opCmp to execute on a struct
> without calling it explicitly:
>
> -------------------------------
> import tango.io.Stdout;
>
> struct Foo
> {
> int i;
> int opCmp(Foo f)
> {
> Stdout.formatln("In opCmp");
> return i-f.i;
> }
> }
>
> Foo getFoo()
> {
> return Foo(1);
> }
>
> void main()
> {
> bool result;
> Stdout.formatln("{}", result);
>
> result = (Foo(1) == Foo(1));
> Stdout.formatln("{}", result);
>
> result = (getFoo() == getFoo());
> Stdout.formatln("{}", result);
>
> Foo f1 = Foo(1);
> Foo f2 = Foo(1);
> result = (f1 == f2);
> Stdout.formatln("{}", result);
>
> result = (f1.opCmp(f2)) == 0;
> Stdout.formatln("{}", result);
> }
> -------------------------------
>
> Output:
> -------------------------------
> false
> true
> true
> true
> In opCmp
> true
> -------------------------------
>
> (FWIW, this is on WinXP 32-bit, DMD 1.043)
>
> Is opCmp completely broken on structs or am I missing something?
I think you have to use opEquals to overload ==. opCmp only applies to
<, <=, >, and >=.
-Lars
More information about the Digitalmars-d-learn
mailing list