C++ boost::函数与函数指针

C++ boost::函数与函数指针,c++,templates,typedef,functor,boost-function,C++,Templates,Typedef,Functor,Boost Function,我正在实现一个通用的设置读取器。 我的想法是,我有一个应用程序,它的设置可以是布尔值、整数和字符串。 然后我有一个Config类,其中实现了这些设置的getter,Config类在构造函数中接受一个客户,这样它就知道它将读取该客户的设置 我很难让它正常工作,我想我误用了boost::function,把它和普通函数指针混淆了 在地图中,我希望有参考,而boost::function只应在配置读取时绑定,因为我已经为给定客户分配了config实例 问题是我不能在没有typedef的情况下使用函数指

我正在实现一个通用的设置读取器。 我的想法是,我有一个应用程序,它的设置可以是布尔值、整数和字符串。 然后我有一个Config类,其中实现了这些设置的getter,Config类在构造函数中接受一个客户,这样它就知道它将读取该客户的设置

我很难让它正常工作,我想我误用了boost::function,把它和普通函数指针混淆了

在地图中,我希望有参考,而
boost::function
只应在配置读取时绑定,因为我已经为给定客户分配了
config
实例

问题是我不能在没有typedef的情况下使用函数指针,这会使模板工作复杂化,有更明智的解决方案吗

#include "Config.h"

class ConfigReader
{

    ConfigReader();

    template<class R>
    R readConfig(std::string customer, std::string settingName);

private:

        typedef bool (Config::* BoolConfigFunctor) () const;
    std::map<std::string, BoolConfigFunctor> boolConfigMap;

        typedef int(Config::* IntConfigFunctor) () const;
    std::map<std::string, IntConfigFunctor> integerConfigMap;

        typedef std::string (Config::* StrConfigFunctor) () const;
    std::map<std::string, StrConfigFunctor> stringConfigMap;

    template<class R>
    std::map<std::string, R (Config::* ) () >  getConfigMap();
}

ConfigReader()
{
    // INIT all settings you want to be readable in the functor maps
    boolConfigMap["USE_NEW_VERSION"]    = &Config::useNewVersion;
    boolConfigMap["MAINTENANCE_MODE"]   = &Config::isMaintenance;
    integerConfigMap["TIMEOUT"]         = &Config::getTimeout;
    stringConfigMap["USERNAME"]         = &Config::getUserName;
            ...
}

template<class R>
R readConfig(std::string customer, std::string settingName)
{
    R returnValue;

    typedef typename std::map<AMD_STD::string,  R (Config::* ) () > ConfigMap_t;
    typedef typename ConfigMap_t::const_iterator ConfigMapIter_t;

    ConfigMap_t configMap = getConfigMap<R>();
    ConfigMapIter_t configIter = configMap.find(settingName);

    if (configIter != configMap.end())
    {
        Config config(customer); // Real instance of Config against which we want to call the function

        boost::function<R ()> configFunction;
        configFunction =
                boost::bind(
                        configIter->second,
                        config);

        returnValue= configFunction();
    }

    return returnValue;
}

template<>
std::map<std::string, bool (Config::* ) ()>  ConfigReader::getConfigMap()
{
    return boolConfigMap;
}

template<>
std::map<std::string, int (Config::* ) ()>  ConfigReader::getConfigMap()
{
    return integerConfigMap;
}

template<>
std::map<std::string, string (Config::* ) ()>  ConfigReader::getConfigMap()
{
    return stringConfigMap;
}
#包括“Config.h”
类ConfigReader
{
ConfigReader();
模板
R readConfig(标准::字符串客户,标准::字符串设置名称);
私人:
typedef bool(配置::*BoolConfigFunctor)()常量;
标准::映射boolConfigMap;
typedef int(配置::*IntConfigFunctor)()常量;
std::map integerConfigMap;
typedef std::string(配置::*strconfig函数)()常量;
std::map-stringConfigMap;
模板
std::map getConfigMap();
}
ConfigReader()
{
//初始化要在函子映射中可读的所有设置
boolConfigMap[“使用新版本”]=&Config::使用新版本;
boolConfigMap[“维护模式”]=&Config::isMaintenance;
integerConfigMap[“TIMEOUT”]=&Config::getTimeout;
stringConfigMap[“用户名”]=&Config::getUserName;
...
}
模板
R readConfig(std::string客户,std::string设置名称)
{
R返回值;
typedef typename std::map ConfigMap\t;
typedef typename ConfigMap\u t::const\u迭代器ConfigMapIter\u t;
ConfigMap\u t ConfigMap=getConfigMap();
ConfigMapIter\u t configIter=configMap.find(设置名称);
if(configIter!=configMap.end())
{
Config Config(customer);//要对其调用函数的配置的真实实例
boost::函数configFunction;
配置函数=
boost::bind(
配置器->第二,
配置);
returnValue=configFunction();
}
返回值;
}
模板
std::map ConfigReader::getConfigMap()
{
返回boolConfigMap;
}
模板
std::map ConfigReader::getConfigMap()
{
返回整数配置图;
}
模板
std::map ConfigReader::getConfigMap()
{
返回stringConfigMap;
}
更新
它通过在映射中使用函数引用而不是boost::function来工作,除非成员函数是静态的,否则不能将成员函数指针用作普通函数指针。您应该改为与特定对象实例一起使用:

boolConfigMap["USE_NEW_VERSION"] = boost::bind(&Config::useNewVersion, someInstanceOfConfig);
(非静态)成员函数指针与普通函数指针(或静态成员函数指针)不同的原因是成员函数有一个隐藏的“zeroteh”参数,即成员函数内的
this
指针

另外,您对
boost::function
对象的声明应该仅为

boost::function<bool()>

是的,我也一直在考虑这个解决方案,但在那个阶段我不能有一个配置实例,因为我可能有很多客户,这就是为什么我只想在配置读取时分配它,然后去掉它it@codeJack那么您就不能真正使用
boost::function
(或者
std::function
),但是必须使用成员函数指针。将更新我的答案。无论如何,考虑到我需要typedef这样的函数指针,并且我有很多模板函数,我可以实现这一点,我一直在尝试,但没有成功。我会用这个更新这个问题。谢谢
(config.*configIter->second)();