我讀了C++ primer第5版,它說最新的標準支持列表初始值設定項。初始化程序列表在C++ 11中是否合法?
我的測試代碼是這樣的:
#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;
}
然後我用鏘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
並編譯如下:
c++ -std=c++11 -Wall playground.cc -o playground
然而,抱怨這樣:
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(),
我檢查了C++ support status of Clang,它看起來應該已經支持Cla 3.1中的初始化程序列表。但爲什麼我的代碼不起作用。有沒有人有關於此的想法?
我想這只是沒有更新庫的初始化列表構造函數。 – chris 2013-02-09 16:51:36
@chris謝謝!有沒有辦法輕鬆更新庫? – 2013-02-09 16:53:42
我不能說。不幸的是,我已經在Windows上完成了所有的工作,沒有Clang。 – chris 2013-02-09 16:57:20