分配给结构映射的语法
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) 接受 C++03 中的这种赋值语法,但抱怨“扩展初始化语法仅在 C++0x 中可用”(释义)。我知道我可以创建一个按顺序接受值的构造函数,但我想知道这种语法是否可以接受(w/o C++0x),或者 G++ 只是安抚我,因为我对 C+ 非常着急+0x。
使用 clang++ 编译甚至不允许它(w/ C++0x,我假设缺少初始化列表),说“预期表达式”,这基本上意味着“那是什么鬼?”,甚至当我使用构造函数(上面注释掉了),它会抛出错误。
摘要:那么基本上,正确的语法是什么?我可以使用初始化列表格式(在 C++03 或 C++0x 中)吗?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上述代码仅在使用新的统一初始化语法的 C++0x 中合法。 C++03 将拒绝这一点。在此期间,为您的
struct
定义一个构造函数是可行的方法。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.