Using initialization lists with std :: map

I asked an earlier question that came out of topic in CString and Unicode.
Now I have reduced my example to namespace std and cout (instead of printf ).
But the main problem still remains.

This is related, but separately from the question, nominated as a duplicate . This question concerns cards in cards and older than 2 years, given that the problem is a priority for the compiler team. (Clearly, this is not a priority)
This question is worth staying open.

Am I using initializers correctly?
Is there an easy way to fix this without a major workaround?
(This is a minimal example based on a much more complex program)

 #include <map> #include <string> #include <iostream> struct Params { int inputType; std::string moduleName; }; int main() { std::map<std::string, Params> options{ { "Add", { 30, "RecordLib" } }, { "Open", { 40, "ViewLib" } }, { "Close", { 50, "EditLib" } }, { "Inventory", { 60, "ControlLib"} }, { "Report", { 70, "ReportLib" } } }; for (const auto& pair : options) { std::cout << "Entry: " << pair.first << " ==> { " << pair.second.moduleName << " }" << std::endl; } return 0; } 

Exit

 Entry: ==> { } Entry: Report ==> { } 

You can see only the final line "Report" .

It really seems to me that the intializer list for std::map just broken.

I am using Microsoft Visual Studio 2013 with Unicode.
This happens on both Debug and Release lines, with Optimizations Disabled or /O2 The same code works fine on IDEOne

+4
c ++ c ++ 11 visual-studio visual-studio-2013
source share
1 answer

In Slava perseverance, I worked with ctors to find an easy fix:

 #include <map> #include <string> #include <iostream> struct Params { int inputType; std::string moduleName; Params(const int n, const std::string& s) : inputType(n), moduleName(s) { } }; int main() { std::map<std::string, Params> options = { { "Add", Params(30, "RecordLib" ) }, { "Open", Params(40, "ViewLib" ) }, { "Close", Params(50, "EditLib" ) }, { "Inventory", Params(60, "ControlLib") }, { "Report", Params(70, "ReportLib" ) } }; for (const auto& pair : options) { std::cout << "Entry: " << pair.first << " ==> { " << pair.second.moduleName << " }" << std::endl; } return 0; } 

However, the source code should have worked, and this is apparently Microsoft's confirmed bug.

+2
source share

All Articles