开发者

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};开发者_如何学Python

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)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜