Clang 中的初始值设定项列表
initializer list in Clang
今天,Apple更新了Xcode的命令行工具,然后将clang从318.0.58升级到318.0.61。
我尝试使用初始值设定项列表,但无法编译下面的代码。
#include <iostream>
#include <random>
#include <initializer_list>
int main()
{
std::mt19937 rng(time(NULL));
std::initializer_list<double> probabilities =
{
0.5, 0.1, 0.1, 0.1, 0.1, 0.1
};
std::discrete_distribution<> cheat_dice (probabilities);
int a[6] = { };
for ( int i = 0 ; i != 1000; ++i )
{
++a[cheat_dice(rng)];
}
for ( int i = 0; i != 6; ++i )
{
std::cout << i + 1 << "=" << a[i] << std::endl;
}
}
然后,我尝试编译。
$ clang++ -stdlib=libc++ foo.cpp
错误日志
foo.cpp:9:10: error: no member named 'initializer_list' in namespace 'std'
std::initializer_list<double> probabilities =
~~~~~^
foo.cpp:9:33: error: expected '(' for function-style cast or type construction
std::initializer_list<double> probabilities =
~~~~~~^
foo.cpp:9:35: error: use of undeclared identifier 'probabilities'
std::initializer_list<double> probabilities =
^
foo.cpp:10:5: error: expected expression
{
^
foo.cpp:14:46: error: use of undeclared identifier 'probabilities'
std::discrete_distribution<> cheat_dice (probabilities);
^
5 errors generated.
另一方面,我可以使用 gcc-4.7.1-RC-20120606 编译上面的代码。
$ g++ -std=c++11 foo.cpp
苹果的 clang 不支持初始值设定项列表吗?叮当版:
$ clang++ -v
Apple clang version 3.1 (tags/Apple/clang-318.0.61) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.0
Thread model: posix
尝试将-std=c++0x
(@jweyrich正确指出)指定为clang
命令行的一部分。clang
的默认值为 C++98 模式。初始值设定项列表是一项 C++11 功能。
此外,从 clang C++98 和 C++11 支持页面,您可以检查各种新C++标准功能的状态。例如,初始值设定项列表在 3.1(及更高版本)中可用。
使用以下命令编译:
clang++ -stdlib=libc++ -std=c++0x foo.cpp
请注意,-std=c++11
也有效。在我的机器中,运行:
$ clang --version
结果在:
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
相关文章:
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 复制列表初始化的隐式转换的等级是多少
- LNK2038、MSVS2017 MAGMA的原因列表
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- clang格式:如何将构造函数的初始值设定项列表的每个元素保存在单独的行上
- clang astcontext.getParents总是返回一个空列表
- 使用 clang 获取类中的方法列表
- Clang AST:获取构造函数的 CXXCtorInitializer 列表,这些构造函数的声明不是定义
- 如何让 clang-3.1 出现在 XCode 的编译器列表中
- 构造函数、初始化器列表以及g++和clang中的不同行为
- 将 clang 3.1 与初始值设定项列表一起使用
- Clang 中的初始值设定项列表
- 如何使用带有clang绑定的python获取C++头中的方法列表
- 如何使用Clang获取C/ c++程序的所有全局声明列表
- clang静态分析器是否通过从unique_ptrs列表中弹出前面而感到困惑
- 模板类中静态字段的初始化列表出现clang错误