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
C++ 函数指针和类模板_C++_Templates_Function Pointers - Fatal编程技术网

C++ 函数指针和类模板

C++ 函数指针和类模板,c++,templates,function-pointers,C++,Templates,Function Pointers,我有一个使用以下类型的模板类CFoo enum My_Types {t1,t2}; 具有 template<My_Types T> class CFoo { public: CFoo() { std::cerr<<"ERROR:...."; exit(1); } }; template<> class CFoo<t1> { .... }; 如何正确传递此模板函数的指针?您

我有一个使用以下类型的模板类CFoo

enum My_Types {t1,t2};
具有

template<My_Types T>
class CFoo
{
    public:
    CFoo()
    {
        std::cerr<<"ERROR:....";
        exit(1);
    }
};

template<>
class CFoo<t1>
{
    ....
};

如何正确传递此模板函数的指针?

您只需编写
foo1
。或者只是
foo1
(适用于g++4.7.2)。

作业应如下所示:

foo1_pointer = &foo1<CFoo<t1>>;
foo1\u指针=&foo1;

这可能是由于GCC编译器的旧版本造成的。此代码在GCC 4.7.2、Clang 3.2、ICC 13.0.1和VS10上编译良好:

#include <iostream>

enum My_Types {t1,t2};

template<My_Types T>
class CFoo { /* ... */ };

template<> class CFoo<t1> { /* ... */ };

template<class T>
void foo1 (const T &, ...) { /* ... */ }

int main()
{
    void (*foo1_pointer) (const CFoo< t1 >&, ...);
    foo1_pointer = &foo1;
}
#包括
枚举My_类型{t1,t2};
模板
类CFoo{/*…*/};
模板类CFoo{/*…*/};
模板
void foo1(常数T&,…){/*…*/}
int main()
{
void(*foo1_指针)(常量CFoo&,…);
foo1_指针=&foo1;
}
当从分配给它的函数指针类型中获取其地址时,编译器应该能够推断出
foo1
的模板参数。根据C++11标准第14.8.2.3/1段:

当获取重载函数的地址时,可以从指定的类型推断模板参数(13.4)。函数模板的函数类型和指定类型被用作P和A的类型,并按照14.8.2.5中的说明进行推断

此外,根据第13.4/1段:

在某些上下文中,不带参数的重载函数名的使用解析为重载集中特定函数的函数、函数指针或成员函数指针。函数模板名称被认为是在这样的上下文中命名一组重载函数。所选函数的类型与上下文中所需目标类型的函数类型相同。[……]


你的编译器BTW?@ ANDYPROWL:C++(Ubuntu 4.4.3-4Ubuntu5.1)4.4。3@pawel_winzig:我问的是什么编译器,不是操作系统。GCC,叮当声,…@AndyProwl:对不起,输入错误:GCC
foo1_pointer=&foo1;
foo1_pointer = &foo1<CFoo<t1>>;
#include <iostream>

enum My_Types {t1,t2};

template<My_Types T>
class CFoo { /* ... */ };

template<> class CFoo<t1> { /* ... */ };

template<class T>
void foo1 (const T &, ...) { /* ... */ }

int main()
{
    void (*foo1_pointer) (const CFoo< t1 >&, ...);
    foo1_pointer = &foo1;
}