4

I read the C++ primer 5th edition, which says that newest standard support list initializer.

My test code is like this:

#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, it 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 of Clang, and it looks that it should already support Initializer lists in Clang 3.1. But why does my codes doesn't work. Does anyone have ideas about this?

Flexo
  • 87,323
  • 22
  • 191
  • 272
Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • 1
    I guess it's just the library not being updated to have initializer list constructors. – chris Feb 09 '13 at 16:51
  • @chris Thanks! Is there a way to update the library easily? – Hanfei Sun Feb 09 '13 at 16:53
  • I wouldn't be able to say. I've done everything on Windows, without Clang, unfortunately. – chris Feb 09 '13 at 16:57
  • possible duplicate of [On OS X, simple C++ program gives incorrect results (which are a result of command-line options 'c++03' vs 'c++11')](http://stackoverflow.com/questions/14149835/on-os-x-simple-c-program-gives-incorrect-results-which-are-a-result-of-comma) – Jonathan Wakely Feb 09 '13 at 17:00
  • Also http://stackoverflow.com/questions/9345271/xcode-4-3-and-c11-include-paths?rq=1 and the accepted answer there – Jonathan Wakely Feb 09 '13 at 17:03
  • http://liveworkspace.org/code/UedRt$7 your code is working here. must be a library problem – Arpit Feb 09 '13 at 17:05

1 Answers1

6

The code is legal, the problem is with your compiler+stdlib setup.

Apple's Xcode ships with the ancient version 4.2.1 of the GNU C++ standard library, libstdc++ (see https://stackoverflow.com/a/14150421/981959 for details) and that version pre-dates C++11 by many years so its std::vector doesn't have an initializer-list constructor.

To use C++11 features you either need to install and use a newer libstdc++, or tell clang to use Apple's own libc++ library, which you do with the -stdlib=libc++ option.

Community
  • 1
  • 1
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • The OP never said he was using Xcode, in fact that wasn't even implied in the post. While this may be the issue, the result is not from Xcode at any rate. – Richard J. Ross III Feb 09 '13 at 17:02
  • 5
    @RichardJ.RossIII I didn't say he's using Xcode, I said Xcode ships with GCC 4.2.1, which is true. The header include paths in the error show the headers are from GCC 4.2.1, which pre-dates C++11. Why else would someone have installed GCC 4.2.1 if not because it's the version that came with Xcode? – Jonathan Wakely Feb 09 '13 at 17:04
  • Thanks for your help! I understand the reason now. It works after I add `-stdlib=libc++` option – Hanfei Sun Feb 10 '13 at 03:17