locked
Defining == operator on WinRT class RRS feed

  • Question

  • Why is there a restriction on defining the == operator on my custom WinRT classes? I get a compile time error saying that the  operator is not allowed.
    Wednesday, September 18, 2013 3:23 PM

Answers

  • Hi Jim,

    I assume you're running into C2919 here ("Operators cannot be used on the published surface of a WinRT type").

    The published surface of the type has to be ABI compliant and representable via COM so it can be projected into all languages. I believe built-in Windows Runtime Classes which appear to override operators actually do so at the projection level rather than in the runtime class itself.

    You can override operator== for use within the class by marking it protected or internal.

    --Rob

    Wednesday, September 18, 2013 10:14 PM
    Moderator

All replies

  • Do you have some code you can share?  I can define that operator!

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, September 18, 2013 9:30 PM
    Moderator
  • Hi Jim,

    I assume you're running into C2919 here ("Operators cannot be used on the published surface of a WinRT type").

    The published surface of the type has to be ABI compliant and representable via COM so it can be projected into all languages. I believe built-in Windows Runtime Classes which appear to override operators actually do so at the projection level rather than in the runtime class itself.

    You can override operator== for use within the class by marking it protected or internal.

    --Rob

    Wednesday, September 18, 2013 10:14 PM
    Moderator
  • Thanks Rob,

    Yes that's the error message I'm getting back. Unfortunately I need the comparison outside of the class, so I'll have to address the problem by some other means.

    Thursday, September 19, 2013 12:39 AM
  • You can provide an explicit comparison function (e.g. String::CompareOrdinal ).

    --Rob

    Thursday, September 19, 2013 2:28 AM
    Moderator