我有以下的类在我的.h文件中声明模板函数:如何正确结合型的别名,函数指针,并
class BeliefCondFunc
{
private:
using funcTypeBool = bool(*)(const bool&, const std::list<bool>&, const std::vector<bool>&);
using funcTypeInt = bool(*)(const int&, const std::list<int>&, const std::vector<int>&);
using funcTypeFloat = bool(*)(const float&, const std::list<float>&, const std::vector<float>&);
using funcTypeString = bool(*)(const std::string&, const std::list<std::string>&, const std::vector<std::string>&);
static std::unordered_map<std::string, funcTypeBool> const m_FunctionMapBool;
static std::unordered_map<std::string, funcTypeInt> const m_FunctionMapInt;
static std::unordered_map<std::string, funcTypeFloat> const m_FunctionMapFloat;
static std::unordered_map<std::string, funcTypeString> const m_FunctionMapString;
template <typename T>
static bool Greater(const T& Fact, const std::list<T>& Facts, const std::vector<T>& Params)
{
return Params.empty() ? false : (Fact > Params[0]);
}
public:
template <typename T>
static bool Call(const std::string FuncName, const T& Fact, const std::list<T>& Facts, const std::vector<T>& Params)
{
if(typeid(T) == typeid(int))
return m_FunctionMapInt.find(FuncName) != m_FunctionMapInt.end() ? (*m_FunctionMapInt.at(FuncName))(Fact, Facts, Params) : false;
return false;
}
};
而且在.cpp文件中,我定义像这样的const
函数映射:
std::unordered_map<std::string, BeliefCondFunc::funcTypeInt> const BeliefCondFunc::m_FunctionMapInt
{
{ "Greater", &Greater<int> },
};
然而,当我尝试编译这段代码,我得到关于在Call()
方法调用的函数指针以下错误:
error C2664: 'bool (const int &,const std::list<int,std::allocator<_Ty>> &,const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 3 from 'const std::vector<std::string,std::allocator<_Ty>>' to 'const std::vector<int,std::allocator<_Ty>> &'*
任何想法我在这里做错了吗?
'&BeliefCondFunc ::大' –
@RichardHodges不,不会改变任何事情。 – Matthias
@AlexD为什么不,您会推荐什么其他方法? – Matthias