Clang 3.1 and C++11 support status

2019-01-17 11:22发布

问题:

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?

回答1:

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.



回答2:

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.