C++ 结合可变模板定义指向成员的指针数组

C++ 结合可变模板定义指向成员的指针数组,c++,c++11,variadic-templates,pointer-to-member,C++,C++11,Variadic Templates,Pointer To Member,在嵌入式应用程序中,我想创建一个helper类,它包含指向某个类的成员函数的指针列表,helper类在其中连续调用成员函数。目前,我对保存指针的静态数组的definition语句有问题。代码如下: template<class C, class F> struct FunctionSequence; template<class C, class R, class... Args> struct FunctionSequence<C, R(Args...)>

在嵌入式应用程序中,我想创建一个helper类,它包含指向某个类的成员函数的指针列表,helper类在其中连续调用成员函数。目前,我对保存指针的静态数组的definition语句有问题。代码如下:

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在一行之后给出了一个附加错误:

error C3855: 'FunctionSequence<C,R(Args...)>::Type<F...>': template parameter 'F' is incompatible with the declaration
错误C3855:'FunctionSequence::Type':模板参数'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...};
    };
};
模板
结构函数序列
{
typedef R(C::*PointerToMember)(Args…);
模板
结构类型
{
静态constexpr指针成员f[sizeof…(f)]={f…};
};
};

将@dyp注释转换为答案:

不要使用
typename outer::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... };
模板
模板
常量typename FunctionSequence::PointerToMember
FunctionSequence::Type::f[sizeof…(f)]
={F..};

什么是
F
?打包类型?你能扩展代码上下文吗?事实上,我不明白它去了哪里。在
结构类型下
?确实,您的解决方案适用于GCC,谢谢;不幸的是,Visual Studio 2013还不支持类内部的初始化。我正在为这两个编译器寻找解决方案,因此我正在寻找在类外初始化时f的定义有什么问题。@Richard这真的太复杂了。您不知道如何执行此操作。您可以使用静态成员函数(带有静态局部变量)而不是静态数据成员。但是,我感兴趣的是静态数据成员定义的正确语法。我将示例归结为,问题是用作模板参数的
typename outer::type V
。感谢您缩短了问题!我试图在模板定义中放弃使用typedef(请参阅),但仍然会出现编译错误。GCC说“模板参数列表太多”。哇,这是我见过的最丑陋的东西(不是自愿遮掩的):
R(C::*…F)(Args…
当然问题是:使用
typename outer::type V
非法吗?你能做类似的事情吗?(我的意思是,一个非类型模板参数,其类型依赖于依赖于同一模板声明的类型的嵌套类型..)
error C3855: 'FunctionSequence<C,R(Args...)>::Type<F...>': template parameter 'F' is incompatible with the declaration
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...};
    };
};
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... };