Initializing map of maps with initializer list in VS 2013

前端 未结 2 1674
感情败类
感情败类 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> testmap = {
        make_pair(Record::BasementType, std::unordered_map({ { "0", "" }, { "1", "Slab or pier" }, { "2", "Crawl" } })),
        make_pair(Record::BuildingStyle, std::unordered_map({ { "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>(...)

    Maybe this workaround helps someone still using VS2013 like me.

提交回复
热议问题