[英]How to typedef template function pointer?
I want to typedef a function pointer that points to a template function. 我想输入一个指向模板函数的函数指针 。
class A
{
template <typename T>
typedef void (*FPTR)<T>();
}
I have tried in this way and didn't succeed. 我已经尝试过这种方式,但没有成功。 Any idea about this thing?
对这件事有什么想法吗?
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types. 正如@HolyBlackCat指出的那样,普通函数指针应该可以正常工作,因为您有一个简单的模板化void函数,该模板的模板参数对返回和参数类型均无效。
template <typename T>
void someVoidFunction() {}
using fPtrType = void(*)();
int main()
{
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
}
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind. 如果是这种情况,则模板参数取决于函数arg和返回类型,对于每种类型,您也应该实例化函数指针。
template <typename T, typename U>
T someFunction(U u) {}
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
{
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
}
Template functions produce functions. 模板函数产生函数。 Template classes produce classes.
模板类产生类。 Template variables produce variables.
模板变量产生变量。
Pointers can point at functions or variables. 指针可以指向函数或变量。 They cannot point at templates;
他们不能指向模板。 templates have no address.
模板没有地址。
Typedef defines the type of a variable. Typedef定义变量的类型。
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time. 模板变量指针可以共同指向模板函数的各种实例,但是初始绑定将在编译时完成,并且一次只能指向其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.