我认为这是在LLVM/Clang下编译时名称空间'std'中名为'unique_ptr'的No类型问题的一部分.据Marshall Clow说,我可以-stdlib=libc++通过_LIBCPP_VERSION以下方式检测:
如果您正在编写跨平台代码,有时您需要知道您正在使用的标准库.从理论上讲,它们都应该提供相同的功能,但这只是理论.有时你只需要知道.检查libc ++的最佳方法是查找预处理器符号_LIBCPP_VERSION.如果已定义,那么您正在使用libc ++.
Run Code Online (Sandbox Code Playgroud)#ifdef _LIBCPP_VERSION // libc++ specific code here #else // generic code here #endif
不幸的是,在从LLVM项目下载后,我使用Apple的Clang(3.4-SVN)和Clang(3.6)来解决这个问题.我猜这个测试只在Xcode下有效.
如何-stdlib=libc++在预处理器中可靠地检测?
以下是测试用例:
$ cat test-clapple.cxx
// Need to test {C++03,C++11} x {libc++, no libc++}
// c++ -c test-clapple.cxx
// - OK
// c++ -stdlib=libc++ -c test-clapple.cxx
// - OK
// c++ -std=c++11 -c test-clapple.cxx
// - FAILS, no type named 'unique_ptr' in namespace 'std'
// c++ -std=c++11 -stdlib=libc++ -c test-clapple.cxx
// …Run Code Online (Sandbox Code Playgroud)