这是编译器错误吗?难道我做错了什么?

时间:2022-01-02 20:09:46

I'm trying to make a simple map to look up some data, but the results are coming out very strange:

我正在尝试制作一个简单的地图来查找一些数据,但结果很奇怪:

#include "stdafx.h"
#include "atlstr.h"
#include <map>

enum InputTypes { Manual, Automatic, Assisted, Imported, Offline };

struct Params
{
    int inputType;
    const char* moduleName;
    DWORD flag;
};

int _tmain()
{
    std::map<CString, Params> options {
        { "Add",       { Manual,    "RecordLib",  0 } },
        { "Open",      { Assisted,  "ViewLib",    1 } },
        { "Close",     { Imported,  "EditLib",    2 } },
        { "Inventory", { Automatic, "ControlLib", 3 } },
        { "Report",    { Offline,   "ReportLib",  4 } }
    };

    for (std::map<CString, Params>::iterator iter = options.begin(); iter != options.end(); ++iter)
    {
        printf("Entry: %s ==> { %d, %s, %d }\n", (const char*)(iter->first),
            iter->second.inputType, iter->second.moduleName, iter->second.flag);
    }


    return 0;
}

Output:

输出:

Entry: îþîþîþîþîþîþîþîþîþîþîþîþ[â0; t ==> { 0, RecordLib, 0 }
Entry: Close ==> { 3, EditLib, 2 }
Entry: Inventory ==> { 1, ControlLib, 3 }
Entry: îþîþîþîþîþîþîþîþîþîþîþîþCâ0# t ==> { 2, ViewLib, 1 }
Entry: Report ==> { 4, ReportLib, 4 }

As you can see, a couple of the CString values turned to garbage. But I don't see any reason why I couldn't create a map this way.

如您所见,一些CString值变为垃圾。但是我没有看到任何我无法以这种方式创建地图的原因。

Is this a bug in the Microsoft Visual Studio 2013 compiler?
Is there something peculiar about my code that I'm missing?

这是Microsoft Visual Studio 2013编译器中的错误吗?我的代码有什么特别之处吗?

############ Edit ##############

############编辑##############

For those who think this is a problem with CString, I re-wrote it with std::string, and got worse output:

对于那些认为这是CString问题的人,我用std :: string重写了它,输出效果更差:

#include "stdafx.h"
#include "atlstr.h"
#include <map>

enum InputTypes { Manual, Automatic, Assisted, Imported, Offline };

struct Params
{
    int inputType;
    std::string moduleName;
    DWORD flag;
};

int _tmain(int argc, _TCHAR* argv[])
{
    std::map<std::string, Params> options{
        { "Add",       { Manual, "RecordLib", 0 } },
        { "Open",      { Assisted, "ViewLib", 1 } },
        { "Close",     { Imported, "EditLib", 2 } },
        { "Inventory", { Automatic, "ControlLib", 3 } },
        { "Report",    { Offline, "ReportLib", 4 } }
    };

    for (std::map<std::string, Params>::iterator iter = options.begin(); iter != options.end(); ++iter)
    {
        printf("Entry: %s ==> { %d, %s, %d }\n", iter->first.c_str(),
            iter->second.inputType, iter->second.moduleName.c_str(), iter->second.flag);
    }
    return 0;
}

Output

产量

Entry:  ==> { 0, , 0 }
Entry: Report ==> { 4, , 4 }

Note that this does work on IDEOne

请注意,这适用于IDEOne

1 个解决方案

#1


3  

I have copied you example into MSVC. It is even more than incorret printing - it is a memory violation in ATL CString upon destruction of map. But all works with std::string. Conclusion - buggy ATL implementation. If I am to take a wild guess, I'd say, it's a bug in move constructor.

我已将您的示例复制到MSVC中。它甚至不仅仅是打印 - 在破坏地图时它是ATL CString中的内存违规。但所有与std :: string一起使用。结论 - 有缺陷的ATL实现。如果我要猜测,我会说,这是移动构造函数中的一个错误。

#1


3  

I have copied you example into MSVC. It is even more than incorret printing - it is a memory violation in ATL CString upon destruction of map. But all works with std::string. Conclusion - buggy ATL implementation. If I am to take a wild guess, I'd say, it's a bug in move constructor.

我已将您的示例复制到MSVC中。它甚至不仅仅是打印 - 在破坏地图时它是ATL CString中的内存违规。但所有与std :: string一起使用。结论 - 有缺陷的ATL实现。如果我要猜测,我会说,这是移动构造函数中的一个错误。