用于提升的基于模板的包装器函数:bimap 查找无法正常工作

Template-based wrapper function for boost::bimap lookup not correctly working

本文关键字:常工作 工作 查找 于模板 包装 用于 函数 bimap      更新时间:2023-10-16

我有一对函数,简单地说,检索bimap的左/右值并打印一条消息(或者,根据函数的布尔参数,在程序中导致致命错误):

#ifdef __cplusplus
template<typename Lt, typename Rt>
Rt Q_bimapleft( boost::bimap<Lt, Rt> themap, Lt L, bool throwError = false )
{
    try
    {
        Rt returnVal = themap.left.at(L);
        return returnVal;
    }
    catch( ... )
    {
        if( throwError )
        {
            Com_Error(ERR_FATAL, "Q_bimapright failure on lookup of %sn", boost::lexical_cast<char *, Lt>(L));
        }
        else
        {
            Com_Printf(S_COLOR_YELLOW "WARNING: Q_bimapright failure on lookup of %sn", boost::lexical_cast<char *, Lt>(L));
        }
    }
    return (Rt)-1;
}
template<typename Lt, typename Rt>
Lt Q_bimapright( boost::bimap<Lt, Rt> themap, Rt R, bool throwError = false )
{
    try
    {
        Lt returnVal = themap.right.at(R);
        return returnVal;
    }
    catch( ... )
    {
        if( throwError )
        {
            Com_Error(ERR_FATAL, "Q_bimapleft failure on lookup of %sn", boost::lexical_cast<char *, Rt>(R));
        }
        else
        {
            Com_Printf(S_COLOR_YELLOW "WARNING: Q_bimapleft failure on lookup of %sn", boost::lexical_cast<char *, Rt>(R));
        }
    }
    return (Lt)-1;
}
#endif

但是,当我去使用该功能时:

....
boost::bimap<int, std::string> animTable;
char token[1024];
....
int index = Q_bimapleft<int, std::string>(animTable, token);

智能感知/编译器报告:

IntelliSense: no instance of function template "Q_bimapleft" matches the argument list
Q_bimapleft<int, std::string>(animTable, token);

必须具有签名为

(boost::bimap<int, string> themap, std::string)

(boost::bimap<int, string> themap, int)

换句话说,您正在尝试将char*传递为int