I am using a map as an associative array of IDs -> value, where the value is a struct defining the object:
#include <map>
struct category {
int id;
std::string name;
};
std::map<int, category> categories;
int main() {
categories[1] = {1, "First category"};
categories[2] = {2, "Second category"};
}
The above code compiles with g++, but with the following warning:
warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
I have read various questions/answers here about struct initialization, but I'm still a bit confused. I have a series of related questions:
I could add the compiler option -std=c++0x and be done with the warning, but still be none the wiser about the underlying problem. Wouldn't things break if I add a method to the category struct?
What would the best way be to initialize this POD struct (category) in a more C++03 compliant way?
Basically, I am not yet sure of the consequences of doing things one way rather than another way. This kind of associative array (where the key is the ID of an object) is easy with PHP, and I'm still learning about the proper way to do it in C++. Is there anything I should pay attention to in the context of the code above?
Edit
The following questions are related, but I didn't understand the answers when I first read them:
C++ initialize anonymous struct
c++ Initializing a struct with an array as a member
Initializing structs in C++