C++ 这是一个编译器错误吗?我做错什么了吗?
我试图制作一张简单的地图来查找一些数据,但结果非常奇怪:C++ 这是一个编译器错误吗?我做错什么了吗?,c++,dictionary,compiler-bug,C++,Dictionary,Compiler Bug,我试图制作一张简单的地图来查找一些数据,但结果非常奇怪: #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 _tm
#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;
}
正如您所看到的,一些CString值变成了垃圾。
但是我不明白为什么我不能用这种方式创建地图
这是Microsoft Visual Studio 2013编译器中的错误吗?我的代码有什么特别的地方我遗漏了吗 编辑 对于那些认为这是
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;
}
请注意,这确实适用于我已将您的示例复制到MSVC中。它甚至不仅仅是不正确的打印——它是地图被破坏后ATL CString中的内存冲突。但是所有这些都可以使用std::string。结论-错误的ATL实现。如果我要胡乱猜测,我会说,这是move构造函数中的一个bug。您必须将
CString
对象明确地强制转换为LPCTSTR
。不过最好还是一起远离CString
。@Chad,再加上过去一个世纪的所有省略号废话:我已经更新了我的代码,使其具有显式演员阵容。但是,同样的问题依然存在。放弃它,仅在2015年尝试过。您编辑的代码在vs2013中对我有效(除非删除stdafx
和atlstr
,并用main()
替换\tmain(…)
)。32位和64位都更新了我的代码,使用printf,并使用显式转换来证明bug仍然存在。
#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;
}
Entry: ==> { 0, , 0 }
Entry: Report ==> { 4, , 4 }