23

From clang's C++11 support status website, http://clang.llvm.org/cxx_status.html , it says, "Initializer List" and "Lambda Expression" are all supported starting from version 3.1.

However, using LLVM/Clang trunk (3.2), compiling against initializer list and lambda expression will yield error messages.

Does anyone know if Clang >3.1 supports those features?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
will
  • 589
  • 1
  • 7
  • 15
  • possible duplicate of [How can I use C++ 11 features in Clang?](http://stackoverflow.com/questions/10408849/how-can-i-use-c-11-features-in-clang) – Sergey K. Jul 03 '13 at 13:48

2 Answers2

33

By default, clang++ will not enable the C++11 features - you have to pass an additional flag during compilation.

clang++ -std=c++11 [input files...]

Or

# enables some additional C++11 extensions GCC has
clang++ -std=gnu++11 [input files...] 

Additionally, you can switch between using libstdc++ and Clang's own libc++, which are different implementations of the C++ standard library. libc++ in some cases might have a better implementation of the C++11 standard than your existing libstdc++ library.

# uses clang's C++ library in C++98 mode
clang++ -stdlib=libc++ [input] # uses clang's C++ library

# uses clang's C++ library and enables C++11 mode
clang++ -stdlib=libc++ -std=c++11 [input] 

The latter is important if you're using Clang in an environment with an outdated version of libstdc++ (like Mac OSX), but note that the two C++ libraries are not compatible with each other, so you would have to rebuild any dependencies against libc++ if you were to use that.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • 1
    The second one is not for GCC compatibility. GCC also has a strict `C++11` mode. `gnu++11` is for enabling some GNU extensions needed for probably internal code and certainly glibc (that would be `gnu90` or `gnu99` probably). – rubenvb May 15 '12 at 14:38
  • Using the `-std` command line switch will not enable the features the questioner is asking about because they are not yet supported by clang. – Bill Weinman May 22 '12 at 21:41
  • @BillWeinman they're supported in the recent 3.1 release and have been on the trunk for some time now. – bames53 May 23 '12 at 21:41
0

The page at http://clang.llvm.org/cxx_status.html is confusing at best. Currently, the released 3.1 version does not support initializer lists or lambdas (so I've switched back to GCC 4.8 for the time being).

You can always check clang support for features using the __has__feature macro, according to the instructions here:

http://clang.llvm.org/docs/LanguageExtensions.html#checking_language_features

For example, __has_feature(cxx_generalized_initializers) or __has_feature(cxx_lambdas) will return true if those features are available and enabled.

Personally, I'm expecting those features to be ready by clang 4.0, which is expected to be released with the next Xcode (likely June 2012).

-- Edited to clarify the versions I've been testing -- clearly, clang versioning is more complex than I had realized.

Bill Weinman
  • 2,036
  • 2
  • 17
  • 12
  • Try clang++ -std=c++11 on2.9 or so onwards. They've got good stuff. – emsr May 16 '12 at 03:47
  • 1
    The questions was specifically about initializer lists and lambda expressions. clang 2.9 does not support those features. – Bill Weinman May 16 '12 at 18:48
  • 2
    Initializer lists and lambdas are both supported by clang 3.1, and have been supported by top of trunk for some time now. Also the status page is and has been kept up to date quite well for a while now. – bames53 May 23 '12 at 21:39
  • 1
    I think you may be confusing Xcode releases with Clang releases. Clang 3.1 (the most recent release) has nearly-complete C++11 support, including lambdas and initializer lists. – Richard Smith May 26 '12 at 07:19
  • 1
    `c++ --version` says `Apple clang version 3.1 (tags/Apple/clang-318.0.58) (based on LLVM 3.1svn)` -- is that not clang 3.1? – Bill Weinman May 26 '12 at 15:19
  • 3
    No, it's not. `Apple clang` versions are not the same as Clang versions. `LLVM 3.1svn` indicates that this is an SVN snapshot from some time between the 3.0 and 3.1 releases. I'm not sure how old that is, but it seems to have been around since at least February. – Richard Smith May 28 '12 at 23:54
  • It supports. PLEASE pass the -std=c++11 TO ENABLE it. – yoco Jun 18 '12 at 07:21