c++ - 与可变参数模板相结合的成员指针数组的定义

标签 c++ c++11 variadic-templates pointer-to-member

在嵌入式应用程序中,我想创建一个辅助类,它包含一个指向某个类的成员函数的指针列表,其中辅助类连续调用成员函数。目前,我在处理保存指针的静态数组的定义语句时遇到了麻烦。这是代码:

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/

相关文章:

c++ - 为什么在boost::bind中指定了参数placer后,调用时省略了?

c++ - 为什么 clang 和 gcc 以不同的方式处理具有类内初始化的结构的支撑初始化?

c++ - 使用声明包含未扩展的参数包

c++ - C++中没有参数可变参数模板函数

c++ - 在模板类中定义的友元函数

c++ - 是否可以从可变参数模板类型参数调用静态方法?

c++ - 如何在运行时从 Windbg 扩展中的 DMP 文件获取类型信息?

c++ - 重载函数的调用 - 以继承类作为参数 - 是不明确的

c++ - 简单的 C++ 指针 : How to link a child node to parent node

c++ - 在执行 SFINAE 时访问模板化派生类 (CRTP) 的静态函数时类型不完整