这是编译器的错误吗?我做错了吗? [英] Is this a compiler bug? Am I doing something wrong?
问题描述
我正在尝试一个简单的地图来查找一些数据,但结果很奇怪:
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;
}
输出:
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 }
正如你所看到的,一些CString值转为垃圾。
但我没有看到任何原因,我无法以这种方式创建地图。
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.
这是Microsoft Visual Studio中的一个错误2013编译器?
有没有什么特别的代码,我错过了?
Is this a bug in the Microsoft Visual Studio 2013 compiler?
Is there something peculiar about my code that I'm missing?
## ##########编辑##############
############ Edit ##############
对于那些谁认为这是 CString
的问题,我用 std :: string
重新写入,输出更差:
For those who think this is a problem with CString
, I re-wrote it with std::string
, and got worse output:
#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 }
请注意,这在 IDEOne
推荐答案
我将您的示例复制到MSVC中。它甚至超过了内容印刷 - 这是ATL CSt中的内存违反,在销毁地图时。但所有的工作与std :: string。结论 - ATL实施错误。如果我想要一个野蛮的猜测,我会说,这是一个移动构造函数的错误。
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.
这篇关于这是编译器的错误吗?我做错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!