Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax in Assigning to Map of structs

struct Structure {
//  Structure(const char* n, int v, bool a) : name(n), value(v), awesome(a) {}
  const char* name;
  int value;
  bool awesome;
};
std::map<const char*, Structure> map;
map["alpha"] = {"Alpha", 0, true};
map["beta"]  = {"Beta",  1, false};
map["gamma"] = {"Gamma", 2, true};

G++ (4.6) accepts this syntax of assigning in C++03, but complains that 'extended initializer syntax is only available in C++0x' (paraphrasing). I know that I can make a constructor that accepts the values in order, but I wanted to know if this syntax is acceptable (w/o C++0x), or is G++ just appeasing me because I'm so anxious for C++0x.

Compiling with clang++ doesn't even allow it (w/ C++0x, I'm assuming initializer lists are lacking), saying 'expected expression', which basically means "What the hell is that?", and even when I used the constructor (commented out above), it throws errors.

Summary: So basically, what is the correct syntax to use and can I use the initializer list format (in C++03 or C++0x)?

like image 709
norcalli Avatar asked Aug 10 '11 08:08

norcalli


1 Answers

The above code is only legal in C++0x by using the new uniform initialization syntax. C++03 will reject this. In the interim, defining a constructor for your struct is the way to go.

like image 135
templatetypedef Avatar answered Nov 04 '22 20:11

templatetypedef