C++/CLI值类约束将无法编译.为什么?

Sim*_*mon 6 generics c++-cli

几个星期前,我的一位同事花了大约两个小时才发现为什么这部分C++/CLI代码无法用Visual Studio 2008进行编译(我只是用Visual Studio 2010测试它......同样的故事).

public ref class Test
{
    generic<class T> where T : value class
        void MyMethod(Nullable<T> nullable)
    {

    }
};
Run Code Online (Sandbox Code Playgroud)

编译器说:错误

1错误C3214:'T':泛型'System :: Nullable'的泛型参数'T'的无效类型参数,不符合约束'System :: ValueType ^'C:\ Users\Simon\Desktop\Projektdokumentation\GridLayoutPanel\Generics\Generics.cpp 11 1泛型

添加ValueType将使代码编译.

public ref class Test
{
    generic<class T> where T : value class, ValueType
        void MyMethod(Nullable<T> nullable)
    {

    }
};
Run Code Online (Sandbox Code Playgroud)

我现在的问题是.为什么?value class和之间有什么区别ValueType

PS:请参阅Cul的Nullable定义:http://msdn.microsoft.com/de-de/library/b3h38hb0.aspx

Sim*_*mon 5

我分析了以下三种方法的IL代码:

generic<class T> where T : value class, System::ValueType
    static void MyMethod(T arg)
{

}

generic<typename T> where T: value class
    static void MyMethod2(T arg)
{

}

generic<typename T> where T: ValueType 
    static void MyMethod3(T arg)
{
}
Run Code Online (Sandbox Code Playgroud)

相应的IL代码,我用.NET-Reflector解组了:

.method public hidebysig 
 static void MyMethod<valuetype ([mscorlib]System.ValueType).ctor T>
(!!T arg) cil managed
{
}


.method public hidebysig 
static void MyMethod2<valuetype .ctor T>(!!T arg) cil managed
{
}


.method public hidebysig
static void MyMethod3<([mscorlib]System.ValueType) T>(!!T arg) cil managed
{
}
Run Code Online (Sandbox Code Playgroud)

这是IL声明Nullable<T>:

.class public sequential ansi serializable sealed beforefieldinit 
Nullable<valuetype (System.ValueType) .ctor T>
    extends System.ValueType
Run Code Online (Sandbox Code Playgroud)

你可以清楚地看到,只有第一种方法的约束与Nullable<T>'s 匹配100%.(顺便说一下:value class似乎暗示了标准构造函数的存在).但是,为什么编译器为(语义上)相同的约束产生不同的IL代码,仍然是个谜.我将询问Microsoft的C++/CLI Gurus以获取更多信息.