Templates C+中的模板专门化失败+;/CLI

Templates C+中的模板专门化失败+;/CLI,templates,c++-cli,Templates,C++ Cli,我在C++/CLI中有以下类: namespace Example { class c1 {}; class c2 {}; public ref class ExampleClass { public: template<typename T> static int StaticIntFunction() { return 0; } t

我在C++/CLI中有以下类:

namespace Example
{
    class c1 {};
    class c2 {};

    public ref class ExampleClass
    {
    public:
        template<typename T> 
        static int StaticIntFunction()
        {
            return 0;
        }

        template<> 
        static int StaticIntFunction<System::String>()
        {
            return 1;
        }

        template<> 
        static int StaticIntFunction<System::Int32>()
        {
            return 2;
        }

        template<> 
        static int StaticIntFunction<Example::c1>()
        {
            return 3;
        }

        template<> 
        static int StaticIntFunction<Example::c2>()
        {
            return 4;
        }

        template<typename T> 
        static System::Type^ StaticTypeFunction()
        {
            return nullptr;
        }

        template<> 
        static System::Type^ StaticTypeFunction<System::String>()
        {
            return System::String::typeid;
        }

        template<> 
        static System::Type^ StaticTypeFunction<System::Int32>()
        {
            return System::Int32::typeid;
        }

        template<> 
        static System::Type^ StaticTypeFunction<Example::c2>()
        {
            return Example::c2::typeid;
        }

        template<> 
        static System::Type^ StaticTypeFunction<Example::c1>()
        {
            return Example::c1::typeid;
        }
    };
}
但是


所有
t
变量应具有唯一值;我做错了什么?

既然您已经确定此代码在复制到另一个文件时可以正常运行,但没有按原来的方式正常运行,那么就很难找出此版本的相同代码运行不正常的原因

当我遇到类似的事情时,我通常会做两件事:

  • 最大输出编译器的警告级别。在这种情况下,这是通过开关
    /W4
    完成的
  • 开始注释代码之外的东西,直到我有了一些有效的东西或者我发现了问题 在本例中,我将注释掉工作代码(
    StaticIntFunction
    ),注释掉除2或3例外的所有损坏代码(保留模板、字符串和int)。然后开始查看代码之前的所有内容(#包括,此处未显示的其他代码)


    我还发现,由于我编写的C#代码比C++/CLI代码多得多,因此我会查找分号的问题。我只知道,对于我来说,我会错过C++/CLI代码中所需的分号,因为我经常用C#和翻译来思考。

    模板静态系统::Type^StaticTypeFunction(){return T::typeid;}
    ?使用您的源代码和VS2012,一切都按照您的预期运行。5个t变量的值不同。@JoelRondeau我希望我的语法或类型^有问题。奇怪的是,如果我将代码复制并粘贴到我的项目的另一部分,它就可以工作了。Visual Studio中是否有编译器开关或其他东西可以帮助我查看更低级的内容?@BenVoigt常规模板按预期工作(如果我删除C1专门化,我将正确获得默认模板函数)。我的问题似乎只是创建专门化。
    System::Type^ t1 = Example::ExampleClass::StaticTypeFunction<float>();
    System::Type^ t2 = Example::ExampleClass::StaticTypeFunction<int>();
    System::Type^ t3 = Example::ExampleClass::StaticTypeFunction<System::String>();
    System::Type^ t4 = Example::ExampleClass::StaticTypeFunction<Example::c1>();
    System::Type^ t5 = Example::ExampleClass::StaticTypeFunction<Example::c2>();
    
    
    int i1 = Example::ExampleClass::StaticIntFunction<float>();
    int i2 = Example::ExampleClass::StaticIntFunction<int>();
    int i3 = Example::ExampleClass::StaticIntFunction<System::String>();
    int i4 = Example::ExampleClass::StaticIntFunction<Example::c1>();
    int i5 = Example::ExampleClass::StaticIntFunction<Example::c2>();
    
    i1 == 0
    i2 == 2
    i3 == 1
    i4 == 3
    i5 == 4
    
    t1 == null
    t2 == t3 == t4 == t5 == Example::c1::typeid