在 VS 2013 中使用初始化列表初始化地图映射

2021-12-31 00:00:00 c++ c++11 stl visual-studio-2013

我正在尝试使用 C++11 初始化地图.我的编译器是 VS 2013 Express.

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

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    {
        Record::BuildingStyle,
        {
            { "0", "" },
            { "1", "Ranch" },
            { "2", "Raised ranch" }
        }
    },
    // ... and so on
};

它已编译,但我在 ntdll.dll 中遇到断点.但是此代码的简化版本:

It's compile but I'm getting breakpoint inside ntdll.dll. However simplified version of this code:

unordered_map<EnumType, unordered_map<string, string>> substitutions = {
    {
        Record::BasementType,
        {
            { "0", "" },
            { "1", "Slab or pier" },
            { "2", "Crawl" }
        }
    },
    // *nothing more*
};

工作正常.

为什么当我在地图上有不止一对时这不起作用?如何做得更好?

Why this doesn't work when I have more than one pair in map? How to do it better?

推荐答案

这是一个已知的编译器错误,http://connect.microsoft.com/VisualStudio/feedback/details/800104/ .编译器会被初始化列表中的临时对象弄糊涂,甚至可以反复销毁单个对象.因为这是无声的糟糕代码生成,我已经要求编译器团队优先解决这个问题.

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.

相关文章