Is the list of initializers similar legal in C ++ 11?

I read the fifth edition of the C ++ primer, which states that the newest standard list of support list initializers.

My test code is as follows:

#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; } 

Then I use 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 

And compile it like this:

 c++ -std=c++11 -Wall playground.cc -o playground 

However, he complains like this:

 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(), 

I checked the C ++ support status for Clang , and it looks like it should already support Initializer lists in Clang 3.1. But why are my codes not working. Anyone have any ideas on this?

+4
source share
1 answer

The code is legal, the problem is setting up your compiler + stdlib.

Apple Xcode ships with the ancient version 4.2.1 of the GNU C ++ standard library, libstdC ++ (see fooobar.com/questions/681512 / ... for more details) and this version until the C ++ 11 date for many years, so its std::vector does not have an initializer list constructor.

To use the features of C ++ 11, you need to either install, or use the new libstdC ++, or say clang, to use Apple's own libC ++ library, which you use with the -stdlib=libc++ option.

+6
source

All Articles