簡體   English   中英

無法訪問const靜態std :: map枚舉結構

[英]can't access const static std::map enum struct

我試圖有一個枚舉結構映射,以保證默認值不存在時的配置值,並保證僅訪問“真實”配置值。(無std :: string get)

所以標題看起來像這樣:

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
}

但是我無法訪問地圖。

對於std::map operator[] 不是 const。

使用at替代。

在C ++ 11之前:

如果您不使用C ++ 11,則必須使用具有const版本的find

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM