在嵌入式应用程序中,我想创建一个辅助类,它包含一个指向某个类的成员函数的指针列表,其中辅助类连续调用成员函数。目前,我在处理保存指针的静态数组的定义语句时遇到了麻烦。这是代码:
template<class C, class F>
struct FunctionSequence;
template<class C, class R, class... Args>
struct FunctionSequence<C, R(Args...)>
{
typedef R(C::*PointerToMember)(Args...);
template<PointerToMember... F>
struct Type
{
static const PointerToMember f[sizeof...(F)];
};
};
template<class C, class R, class... Args>
template<typename FunctionSequence<C, R(Args...)>::PointerToMember... F>
const typename FunctionSequence<C, R(Args...)>::PointerToMember
FunctionSequence<C, R(Args...)>::Type<typename FunctionSequence<C, R(Args...)>::PointerToMember... F>::f[sizeof...(F)]
= { F... };
struct Test
{
void m1(int) {}
void m2(int) {}
FunctionSequence<Test, void(int)>::Type<&Test::m1, &Test::m2> fs;
};
Visual Studio 2013 和 GCC 4.7.3 都在这一行给出错误,我试图在其中定义 f 变量,并使用成员函数指针列表对其进行初始化:
FunctionSequence<C, R(Args...)>::Type<typename FunctionSequence<C, R(Args...)>::PointerToMember... F>::f[sizeof...(F)]
GCC 给出以下错误:
expansion pattern 'typename FunctionSequence<C, R(Args ...)>::PointerToMember' contains no argument packs
too many template-parameter-lists
Visual Studio 给出以下错误:
error C3546: '...' : there are no parameter packs available to expand
error C2146: syntax error : missing ',' before identifier 'F'
error C3545: 'F': parameter pack expects a non-type template argument
此外,Visual Studio 在一行之后给出了一个额外的错误:
error C3855: 'FunctionSequence<C,R(Args...)>::Type<F...>': template parameter 'F' is incompatible with the declaration
我想做的事情有可能吗?我的代码是否错误,是否可以修复?
最佳答案
将@dyp 评论变成答案:
不要使用 typename outer<T>::type V
作为模板参数。
你必须这样声明:
template<class C, class R, class... Args>
template<R(C::*...F)(Args...)>
const typename FunctionSequence<C, R(Args...)>::PointerToMember
FunctionSequence<C, R(Args...)>::Type<F...>::f[sizeof...(F)]
= { F... };
关于c++ - 与可变参数模板相结合的成员指针数组的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21254221/