Using Initializer Lists with std::map

后端 未结 1 1479
离开以前
离开以前 2020-12-31 20:24

I asked an earlier question, which got off-topic in CString and Unicode issues.
I\'ve now reduced my example to namespace std and cout (instead

1条回答
  •  孤城傲影
    2020-12-31 20:56

    At Slava's insistence, I worked with ctors to find an easy fix:

    #include 
    #include 
    #include 
    
    struct Params
    {
        int         inputType;
        std::string moduleName;
        Params(const int n, const std::string& s) :
            inputType(n),
            moduleName(s)
        { }
    };
    
    int main()
    {
        std::map options = {
            { "Add",       Params(30, "RecordLib" ) },
            { "Open",      Params(40, "ViewLib"   ) },
            { "Close",     Params(50, "EditLib"   ) },
            { "Inventory", Params(60, "ControlLib") },
            { "Report",    Params(70, "ReportLib" ) }
        };
    
        for (const auto& pair : options)
        {
            std::cout << "Entry: " << pair.first << " ==> { " << pair.second.moduleName << "    }" << std::endl;
        }
    
        return 0;
    }
    

    However, the original code should have worked, and apparently is an acknowledged bug by Microsoft.

    0 讨论(0)
提交回复
热议问题