尝试使用 clang + libc++(C++11 模式)编译这一行时:
#include <boost/thread.hpp>
clang 发出以下错误:
In file included from test.cpp:1: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread.hpp:13: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/thread.hpp:17: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/pthread/thread_data.hpp:11: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/locks.hpp:18: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/chrono/time_point.hpp:33: /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:353:49: error: constexpr function never produces a constant expression static BOOST_CHRONO_LIB_CONSTEXPR float lowest() ... ^ /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:355:21: note: non-constexpr function 'max' cannot be used in a constant expression return -(std::numeric_limits::max) (); ^ /usr/bin/../lib/c++/v1/limits:443:43: note: declared here _LIBCPP_INLINE_VISIBILITY static type max() _NOEXCEPT {return ... ^ In file included from test.cpp:1: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread.hpp:13: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/thread.hpp:17: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/pthread/thread_data.hpp:11: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/locks.hpp:18: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/chrono/time_point.hpp:33: /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:361:50: error: constexpr function never produces a constant expression static BOOST_CHRONO_LIB_CONSTEXPR double lowest() ... ^ /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:363:21: note: non-constexpr function 'max' cannot be used in a constant expression return -(std::numeric_limits::max) (); ^ /usr/bin/../lib/c++/v1/limits:443:43: note: declared here _LIBCPP_INLINE_VISIBILITY static type max() _NOEXCEPT {return ... ^ In file included from test.cpp:1: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread.hpp:13: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/thread.hpp:17: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/pthread/thread_data.hpp:11: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/thread/locks.hpp:18: In file included from /Users/yongwei/Programming/boost_1_52_0/boost/chrono/time_point.hpp:33: /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:369:55: error: constexpr function never produces a constant expression static BOOST_CHRONO_LIB_CONSTEXPR long double lowest() ... ^ /Users/yongwei/Programming/boost_1_52_0/boost/chrono/duration.hpp:371:21: note: non-constexpr function 'max' cannot be used in a constant expression return -(std::numeric_limits::max)(); ^ /usr/bin/../lib/c++/v1/limits:443:43: note: declared here _LIBCPP_INLINE_VISIBILITY static type max() _NOEXCEPT {return ... ^ 3 errors generated.
在我看来,这是 libc++ 中的一个错误,因为 C++11 确实要求这些函数为 constexpr
。
有人遇到过同样的问题吗?你知道有什么修复吗?
我的 clang 版本是:
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) Target: x86_64-apple-darwin11.4.2 Thread model: posix
最佳答案
根据马克思的提示,我找到了解决方法。在这种情况下,定义 BOOST_NO_CXX11_NUMERIC_LIMITS
会有所帮助。所以这看起来确实是 libc++ 的不完整性。
根据 Howard Hinnant 的说法,这是 libc++ 的一个 bug,并且已经在 trunk 上修复了。然而,目前尚不清楚 Apple 何时可以将修复程序发布到 Xcode 中。我将暂时标记已回答的问题。
编辑:该问题已在 Apple LLVM 版本 4.2 (clang-425.0.28)(基于 LLVM 3.2svn)中修复。我当前的 Xcode 版本是 4.6.3。
关于c++ - boost/thread.hpp 失败并显示 'clang++ -std=c++11 -stdlib=libc++',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300136/