Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates 使用模板方法指定函数指针?_Templates_Pointers_Visual C++_C++ Cli - Fatal编程技术网

Templates 使用模板方法指定函数指针?

Templates 使用模板方法指定函数指针?,templates,pointers,visual-c++,c++-cli,Templates,Pointers,Visual C++,C++ Cli,考虑下面的实用程序类: 它将包含许多具有相同签名int函数(inta,intb)的工厂方法 实用程序.h class Utility { public: template<typename T, typename A, typename B> static int get_function(T% function, int% number) { function = (T) add; number = 10;

考虑下面的实用程序类:

它将包含许多具有相同签名
int函数(inta,intb)的工厂方法

实用程序.h

class Utility
{
 public:

    template<typename T, typename A, typename B> 
    static int get_function(T% function, int% number)
    {
        function = (T) add;
        number = 10;
        return 0;
    }

    static int add (int a, int b) {
        return a+b;
    }
};
我想要实现的是,通过模板方法(
Utility:get_function
)分配函数指针(
FPClass::fp

但是被调用函数(
实用程序:get_函数
)不会修改函数指针参数(始终为null)。
为什么会这样?

你用什么语言?它是C++/CLI吗?它是托管代码和本机代码的一种非常奇怪的混合,调试器太混乱,无法正确显示“fp”的值。但它确实有效。模板中的(T)演员非常邪恶。谢谢@HansPassant。。我没有继续执行,在调试阶段就停止了:)关于奇怪的混合,我必须这样做,因为我必须使用函数指针和托管代码端的其他功能,如垃圾收集、集合、.Net类等:)@HansPassant你能在这里添加这个作为答案吗?
typedef int (__stdcall *FP) (int a, int b);


ref class FPClass
{    
    public:
        static FP fp;

        static int number;

        static void apply_function()
        {
            Utility::get_function<FP, int, int>(FPClass::fp, number);
            cout <<  FPClass::fp(25, 45);
        }
};


void main()
{
    FPClass::apply_function();
}