这可能是由于您的 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;
}
foo1
当从分配给它的函数指针的类型中获取其地址时,编译器应该能够推断出模板参数。根据 C++11 标准的第 14.8.2.3/1 段:
模板参数可以从获取重载函数(13.4)地址时指定的类型推导出来。函数模板的函数类型和指定类型作为P和A的类型,推导如14.8.2.5所述。
此外,根据第 13.4/1 段:
A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. A function template name is considered to name a set of overloaded functions in such contexts. The function selected is the one whose type is identical to the function type of the target type required in the context. [...]