C++ 为什么我收到“无法使用初始化列表初始化非聚合”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8920699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why am I getting "non-aggregate cannot be initialized with initializer list"
提问by joels
std::map<std::string, int> m = {
{"Marc G.", 123},
{"Zulija N.", 456},
{"John D.", 369}
};
In Xcode I have C++ Language Dialect set to "C++0x [-std=c++0x]" and C++ Standard Library set to "libc++ LLVM C++ Standard library with C++0X Support"
在 Xcode 中,我将 C++ Language Dialect 设置为“C++0x [-std=c++0x]”,并将 C++ 标准库设置为“libc++ LLVM C++ Standard library with C++0X Support”
Do I need to set something else?
我需要设置其他东西吗?
回答by Firoze Lafeer
EDIT 10/2013: this answer is really old, Apple Clang does support this now.
编辑 10/2013:这个答案真的很旧,Apple Clang 现在确实支持这个。
C++11 Initializer Lists are not yet supported as of Clang 3.0
从 Clang 3.0 开始,尚不支持 C++11 初始值设定项列表
See the implementation status here: Clang C++11 implementation status
在这里查看实现状态:Clang C++11 implementation status
(Your syntax looks correct if only the compiler supported this feature)
(如果只有编译器支持此功能,您的语法看起来是正确的)
回答by BartoszKP
Clang 3.6.2 supports initializer-list syntax. Just remember to pass -std=c++11
flag when compiling.
Clang 3.6.2 支持初始化列表语法。只要记住-std=c++11
在编译时传递标志。