如何通过元程序正确地创建成员模板函数指针的数组 [英] How to properly create an array of member template function pointers via meta programming
问题描述
我想预先创建一个模板成员函数指针数组,它将使用在运行时由另一个类确定的变量进行索引。然后可以专门化模板函数。
这在非模板类
中有效,但是我在模板类中遇到问题。应用程序有多个电源,为 MatchT
定义不同的模板参数,这在代码示例中是有希望的代表:
#include Data.H // POD对象。
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>
结构表< 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
#includeManager.H
模板< typename MatchT>
class Process
{
public:
void send(Manager< MatchT>& mgr);
private:
typename Manager< MatchT> :: template table< 4> _processtable;
};
//process.c
模板< typename MatchT>
void Process< MatchT> :: send(Manager< MatchT>& mgr)
{
错误这里:
mgr。* _processtable :: fns [1]
}
ERROR:
在constexpr bool(* const Manager< MyManager< MyConfig>> :: table< 0,0,1,2,3& :: fns [4](data&)
需要从process.C...
需要从manager.H错误没有匹配转换函数'do' 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]
需要从process.C...
manager.H错误没有匹配转换函数'do'到类型'bool(* const)(struct Data&)'
static constexpr bool(* fns [])(Data&)= {do& ...};
注意:候选是:template< int P> bool Manager< MatchT> :: do(Data&)[with int P = P; MatchT = MyMatch< MyConfig>我想知道是否有静态的 static constexpr bool(* fns [])( Data&)= {do&Ps; ...};
在模板类中引起问题。是否不能将每个 Manager< MatchT>
类型的函数定义为静态类型? 解决方案您声明并定义了 fns
当它真的需要一个指向成员函数的指针数组时,函数的指针数组。尝试:
模板< 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(Manager :: * fns [])(Data&)= {& Manager :: do&Ps; ...}
};
};
template< typename MatchT>
template< int ... Ps>
constexpr bool(Manager< MatchT> :: * Manager< MatchT> :: table< 0,Ps ...> :: fns [sizeof ...(Ps)])
I want to pre create an array of template member function pointers, which will be indexed using a variable determined at run time by another class. The template functions can then be specialized.
This works in a non-template class
, however I'm having issues in a template class. The applications has multiple mains which define different template arguments for MatchT
, and this is hopefully representative in the code example:
#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> >]
I'm wondering if having the static static constexpr bool(*fns[])(Data&)={do<Ps>...};
in the template class is causing an issue. Is it not able to define the functions as static for each Manager<MatchT>
type? What am I missing?
解决方案 You declared and defined fns
as an array of pointers to functions, when it really needs to be an array of pointers to member functions. Try:
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(Manager::*fns[])(Data&)={&Manager::do<Ps>...};
};
};
template <typename MatchT>
template<int... Ps>
constexpr bool(Manager<MatchT>::*Manager<MatchT>::table<0,Ps...>::fns[sizeof...(Ps)])(Data&);
这篇关于如何通过元程序正确地创建成员模板函数指针的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!