无法访问常量静态标准::映射枚举结构



我试图有一个枚举结构映射,以保证默认值配置值,如果它不存在,并保证只访问"真实"配置值。(无std::string get)

所以Header看起来像这样:

enum ConfigValues
{
    LOG_LEVEL,
};
class Config
{
public:
    std::string get(const ConfigValues& key);
private:
    struct ConfigMapping
    {
        std::string configKeyString;
        std::string defaultValue;
    };
    const static std::map<ConfigValues, ConfigMapping> m_mapping;
}

cpp包含如下内容:

const std::map<ConfigValues, Config::ConfigMapping> Config::m_mapping=
{
    {LOG_LEVEL, { "logLevel", "5" } },
};
std::string Config::get(const ConfigValues& key)
{
    std::string key = m_mapping[key].configKeyString; // <-- Does not work
}

但是我不能访问地图

operator[]不是对于std::map的const

at代替

c++ 11之前:

如果你不使用c++ 11,你必须使用find,它有一个const版本。

最新更新