Ric*_*ard 5 c++ pointer-to-member variadic-templates c++11
在嵌入式应用程序中,我想创建一个辅助类,它包含某个类的指向成员函数的列表,其中辅助类连续调用成员函数.目前,我遇到了包含指针的静态数组的定义语句的麻烦.这是代码:
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;
};
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2013和GCC 4.7.3都给出了这一行的错误,我试图定义f变量,并用成员函数指针列表初始化它:
FunctionSequence<C, R(Args...)>::Type<typename FunctionSequence<C, R(Args...)>::PointerToMember... F>::f[sizeof...(F)]
Run Code Online (Sandbox Code Playgroud)
GCC给出以下错误:
expansion pattern 'typename FunctionSequence<C, R(Args ...)>::PointerToMember' contains no argument packs
too many template-parameter-lists
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
此外,Visual Studio在一行之后会出现一个额外的错误:
error C3855: 'FunctionSequence<C,R(Args...)>::Type<F...>': template parameter 'F' is incompatible with the declaration
Run Code Online (Sandbox Code Playgroud)
甚至可能我想做什么?我的代码是错的,是否可以修复?
将@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... };
Run Code Online (Sandbox Code Playgroud)