当T实现类时,C ++ / CLI中的IComparable <T>

问题描述

|| 更新:如评论中所述,C ++ / CLI应该为“ 0”;编译器错误“清楚地”指出“必须是值类型”。 在C#中,我可以写
      public struct Id<T> : IComparable<Id<T>>
    {
        public int Compareto(Id<T> other)
        {
            throw new NotImplementedException();
        }
    }
当我尝试在C ++ / CLI中执行相同操作时
generic<typename T>
public ref struct Id : System::IComparable<Id<T>>
{
public:
    virtual int Compareto(Id<T> other)
    {
        throw gcnew System::NotImplementedException();
    }
};
我收到编译器错误
error C3225: generic type argument for \'T\' cannot be \'...::Id<T>\',it must be a value type or a handle to a reference type
。 这是仍然无法解决的编译器错误吗?     

解决方法

        在C ++ / CLI中,您需要在托管的引用类型上使用句柄。这样编译:
generic<typename T>
public ref struct Id : System::IComparable<Id<T>^>
{
public:
    virtual int CompareTo(Id<T>^ other)
    {
        throw gcnew System::NotImplementedException();
    }
};
    ,        如评论中所述; C#的
struct
的C ++ / CLI等效值为
value struct
,而不是
ref struct
。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...