公告
财富商城
积分规则
提问
发文
2020-06-23 06:42发布
来,给爷笑一个
How to write a function pointer as template?
template <typename T> T (*PtrToFunction)(T a);
If you mean create a type for that function, you could do something like this:
template<typename T> struct Function { typedef T (*Ptr)(T); };
Then use it like
int blah(Function<int>::Ptr a) { }
It's ok on Visual Studio 2015 and on GCC you should use command line option -std=c++11
-std=c++11
template<class T> using fpMember = void (T::*)();
I am assuming you are trying to declare a type (you cannot declare a "template variable" without a concrete type).
C++03 doesn't have template typedefs, you need to use a struct as a workaround:
template <typename T> struct FuncPtr { typedef T (*Type)(T a); }; ... // Use template directly FuncPtr<int>::Type intf; // Hide behind a typedef typedef FuncPtr<double>::Type DoubleFn; DoubleFn doublef;
C++11 template aliases will eliminate the struct workaround, but presently no compilers except Clang actually implement this.
template <typename T> typedef T (*FuncPtr)(T a); // Use template directly FuncPtr<int> intf; // Hide behind a typedef typedef FuncPtr<double> DoubleFn; DoubleFn doublef;
You can not do that. You can only create function pointers with concrete type.
最多设置5个标签!
If you mean create a type for that function, you could do something like this:
Then use it like
It's ok on Visual Studio 2015 and on GCC you should use command line option
-std=c++11
I am assuming you are trying to declare a type (you cannot declare a "template variable" without a concrete type).
C++03 doesn't have template typedefs, you need to use a struct as a workaround:
C++11 template aliases will eliminate the struct workaround, but presently no compilers except Clang actually implement this.
You can not do that. You can only create function pointers with concrete type.