Initializing map of maps with initializer list in VS 2013

前端 未结 2 1671
感情败类
感情败类 2020-12-05 20:56

I\'m trying to initialize map of maps using C++11. My compiler is VS 2013 Express.

unordered_map> substit         


        
相关标签:
2条回答
  • 2020-12-05 21:14

    A workaround for this I've discovered over the years is using make_pair in the initializer list instead of the bracket initializer per pair ({ ... }):

    std::unordered_map<Record, std::unordered_map<std::string, std::string>> testmap = {
        make_pair(Record::BasementType, std::unordered_map<std::string, std::string>({ { "0", "" }, { "1", "Slab or pier" }, { "2", "Crawl" } })),
        make_pair(Record::BuildingStyle, std::unordered_map<std::string, std::string>({ { "0", "" }, { "1", "Slab or pier" }, { "2", "Crawl" } })),
        // ... and so on
    };
    

    The compiler seems to be able to deal with the pairs nicely from there regardless of the issue with temporary initializers.

    Note that in your case you have to explicitly cast the inner unordered_map initializer because it could be more than one stl container type being initialized like that. Alternatively you can supply template types on make_pair which has the same casting result: make_pair<Record, std::unordered_map<std::string, std::string>>(...)

    Maybe this workaround helps someone still using VS2013 like me.

    0 讨论(0)
  • 2020-12-05 21:20

    This is a known compiler bug, http://connect.microsoft.com/VisualStudio/feedback/details/800104/ . The compiler gets confused by temporaries in initializer lists, and can even destroy an individual object repeatedly. Because this is silent bad codegen, I've asked the compiler team to prioritize fixing this.

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