简体   繁体   中英

IComparable<T> in C++/CLI when T is implementing class

Update: as said in the comments, the C++/CLI should have been value struct ; the compiler error 'clearly' stated "must be a value type".


In C#, I can write

      public struct Id<T> : IComparable<Id<T>>
    {
        public int CompareTo(Id<T> other)
        {
            throw new NotImplementedException();
        }
    }

When I try to do the same in C++/CLI

generic<typename T>
public ref struct Id : System::IComparable<Id<T>>
{
public:
    virtual int CompareTo(Id<T> other)
    {
        throw gcnew System::NotImplementedException();
    }
};

I get a compiler error error C3225: generic type argument for 'T' cannot be '...::Id<T>', it must be a value type or a handle to a reference type .

Is it this compiler error which still isn't fixed?

In C++/CLI, you need to use handles on the managed, reference types. This compiles:

generic<typename T>
public ref struct Id : System::IComparable<Id<T>^>
{
public:
    virtual int CompareTo(Id<T>^ other)
    {
        throw gcnew System::NotImplementedException();
    }
};

As stated in the comments; the C++/CLI equivalent of C#'s struct is value struct , not ref struct .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM