Visual Studio 2013 Update 2 has just fixed this bug.
Static const map initialization list with struct?
-
27-06-2023 - |
Question
Code section:
struct id_s {
std::string name1;
std::string name2;
};
static const std::map<uint8_t, id_s> list = {
{ 0x00, { "Fred", "Apple" } },
{ 0x01, { "John", "Banana" } },
{ 0x02, { "Mark", "Mango" } }
};
int main()
{
for(const auto& it: list) {
std::cout<<it.first<<"\t"<<it.second.name1<<"\t"<<it.second.name2<<std::endl;
}
return 0;
}
Problem is that once initialized all fields are correctly assigned, except "John" and "Mark", which are left as "".
How to correctly assign this with C++11 initialization lists?
Output from VS 2013 debug console:
Solution 2
OTHER TIPS
This is the correct way to initialize.I have ran your program and used to print it on console using the following code
for(const auto& it: lst) {
std::cout<<it.first<<"\t"<<it.second.name1<<"\t"<<it.second.name2<<std::endl;
}
Output
Fred Apple
0x1 John Banana
0x2 Mark Mango
I ran this program Linux machine. I am using gcc4.8.1 which is fully c++11 compliant.Well I have not used the VS2013 but if you are using it as above and not getting the correct initialized values, there could be problem/bug VS2013 as it may not have complete implementation of initializer list.
BTW you should not use the variable name which has defined in the standard(list). This is not a good practice.
EDIT
it looks like it is known bug in VS2013 and you can find the relevant information from the following SO post.