14

Today Apple updates Command Line Tools for Xcode and then upgrades clang from 318.0.58 to 318.0.61.

I've tried to use initializer list, but can't compile below code.

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

Then, I tried to compile.

$ clang++ -stdlib=libc++ foo.cpp 

Error log

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. 

On the other hand, I can compile above code with gcc-4.7.1-RC-20120606.

$ g++ -std=c++11 foo.cpp 

Doesn't Apple's clang support initializer list? Clang version:

$ 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 

2 Answers 2

15

Try by specifying -std=c++0x (as @jweyrich correctly pointed out) as part of the clang command line. The default for clang is C++98 mode. Initializer lists are a C++11 feature.

Also, from the clang C++98 and C++11 support page you can check the status of various new C++ standard features. For example, initializer lists are available in 3.1 (and above).

Sign up to request clarification or add additional context in comments.

8 Comments

Apple's clang can't recognize -std=c++11
@user1214292: It recognizes -std=c++0x though.
@jweyrich But the result will be unchanged :(
@user1214292: as mentioned in the current answer, the Clang version (3.0) shipped by Apple does not support initializer lists.
@jweyrich $ 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
|
8

Compile using the command:

clang++ -stdlib=libc++ -std=c++0x foo.cpp 

Note that -std=c++11 also works. In my machine, running:

$ clang --version 

results in:

Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) Target: x86_64-apple-darwin12.2.0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.