【发布时间】:2015-10-27 22:39:37
【问题描述】:
我想预先创建一个模板成员函数指针数组,它将使用另一个类在运行时确定的变量进行索引。然后可以专门化模板函数。
这适用于non-template class,但是我在模板类中遇到了问题。应用程序有多个 main,它们为 MatchT 定义了不同的模板参数,希望这在代码示例中具有代表性:
#include Data.H // POD object.
template<typename MatchT>
class Manager
{
public:
template <int P>
bool do(Data& d_);
template<int P, int ...Ps>
struct table : table<P-1, P-1, Ps... >{};
template<int... Ps>
struct table <0, Ps...>
{
static constexpr bool(*fns[])(Data&)={do<Ps>...};
};
};
template <typename MatchT>
template<int... Ps>
constexpr bool(*Manager<MatchT>::table<0,Ps...>::fns[sizeof...(Ps)])();
//process.h
#include "Manager.H"
template<typename MatchT>
class Process
{
public:
void send(Manager<MatchT>& mgr );
private:
typename Manager<MatchT>::template table<4> _processtable;
};
//process.c
template<typename MatchT>
void Process<MatchT>::send(Manager<MatchT>& mgr)
{
ERROR here:
mgr.*_processtable::fns[1]();
}
ERROR:
In instantiation of "constexpr bool (* const Manager<MyManager<MyConfig> >::table<0,0,1,2,3>::fns[4](Data&)
required from "process.C"...
required from "manager.H" error no matches converting function 'do' to type 'bool (* const)(struct Data&)'
static constexpr bool(*fns[])(Data&) = {do<Ps>...};
note:candidate is: template<int P> bool Manager<MatchT>::do(Data&) [with int P = P; MatchT = DummyMatch]
required from "process.C"...
required from "manager.H" error no matches converting function 'do' to type 'bool (* const)(struct Data&)'
static constexpr bool(*fns[])(Data&) = {do<Ps>...};
note:candidate is: template<int P> bool Manager<MatchT>::do(Data&) [with int P = P; MatchT = MyMatch<MyConfig> >]
我想知道模板类中的静态static constexpr bool(*fns[])(Data&)={do<Ps>...}; 是否会导致问题。是否无法将每个 Manager<MatchT> 类型的函数定义为静态函数?我错过了什么?
【问题讨论】:
-
你的意思是
do是static吗?非静态成员函数不能转换为函数指针。 -
啊没听懂。我不能做静态,因为函数中的代码引用了我也不能静态的其他成员。如果我从 fns 的声明中删除 static,我该如何定义它?
-
啊,所以你的意思是
fns是指向成员函数的指针数组,而不是函数指针。 -
是的,如果不清楚,对不起。另外,如果我将 do 设为静态,我不确定如何在 do 中引用实例成员。它只是 Manager
::实例成员?这对每个 MatchT 是否正确? -
实际上我有一个在非模板类上工作的简化示例,其中 processTable 在 main 中声明。指向的函数在该类中不是静态的..
标签: c++ template-specialization template-meta-programming