Han*_*Sun 4 c++ initialization vector clang c++11
我阅读了C++入门第5版,其中说明了最新的标准支持列表初始化程序.
我的测试代码是这样的:
#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ispunct;
int main(int argc, char *argv[])
{
vector<int> a1 = {0,1,2};
vector<int> a2{0,1,2}; // should be equal to a1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用Clang 4.0:
bash-3.2$ c++ --version
Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)
并像这样编译它:
c++ -std=c++11 -Wall playground.cc -o playground
Run Code Online (Sandbox Code Playgroud)
但是,它抱怨这样:
playground.cc:13:17: error: no matching constructor for initialization of
'vector<int>'
vector<int> a1 = {0,1,2};
^ ~~~~~~~
/usr/include/c++/4.2.1/bits/stl_vector.h:255:9: note: candidate constructor
[with _InputIterator = int] not viable: no known conversion from 'int'
to 'const allocator_type' (aka 'const std::allocator<int>') for 3rd
argument;
vector(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/4.2.1/bits/stl_vector.h:213:7: note: candidate constructor
not viable: no known conversion from 'int' to 'const allocator_type'
(aka 'const std::allocator<int>') for 3rd argument;
vector(size_type __n, const value_type& __value = value_type(),
Run Code Online (Sandbox Code Playgroud)
我检查了Clang的C++支持状态,看起来它应该已经支持Clang 3.1中的初始化列表.但为什么我的代码不起作用.有没有人有这个想法?
代码是合法的,问题在于您的编译器+ stdlib设置.
Apple的Xcode附带了GNU C++标准库的古老版本4.2.1 libstdc ++(详情请参阅/sf/answers/990529501/),该版本早于C++ 11的版本已有多年的历史它std::vector没有初始化列表构造函数.
要使用C++ 11功能,您需要安装并使用较新的libstdc ++,或者告诉clang使用Apple自己的libc ++库,您可以使用该-stdlib=libc++选项.
| 归档时间: |
|
| 查看次数: |
6787 次 |
| 最近记录: |