是否可以解决?
在嵌入式应用程序中,我想创建一个辅助类,该类容纳特定类的指针到会员函数的列表,其中助手类调用成员函数陆续。目前,我对静态阵列的定义语句遇到了麻烦。这是代码:
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 11中进行此操作?
template<class C, class R, class... Args>
struct FunctionSequence<C, R(Args...)>
{
typedef R(C::*PointerToMember)(Args...);
template<PointerToMember... F>
struct Type
{
static constexpr PointerToMember f[sizeof...(F)] = {F...};
};
};