Yongwei Wu
Yongwei Wu

Reputation: 5582

boost/thread.hpp fails with 'clang++ -std=c++11 -stdlib=libc++'

When trying to compile this one line with clang + libc++ (C++11 mode):

#include <boost/thread.hpp>

clang emits the following errors:

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.

It looks to me this is a bug in libc++, because C++11 does require these functions to be constexpr.

Anyone encountered the same problem and can you concur? Do you know about any fixes?

My clang version is:

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

Upvotes: 2

Views: 1377

Answers (2)

Yongwei Wu
Yongwei Wu

Reputation: 5582

With Marx's hint, I found a workaround. Defining BOOST_NO_CXX11_NUMERIC_LIMITS helps in this case. So it does seem to be an incompleteness of libc++.

According to Howard Hinnant, it is a bug of libc++, and is already fixed on the trunk. However, no info yet when Apple can release the fix into Xcode. I will mark the question answered for now.

EDIT: The issue is fixed in Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn). My current Xcode version is 4.6.3.

Upvotes: 3

Marx Yu
Marx Yu

Reputation: 371

i use boost in xcode 4.3, and use tr1 random, fisrt also build error, but i defined #define BOOST_HAS_TR1_RANDOM 1 before #include and build successfully. you can try the similar way, manually point out the builder have it own tr1 implemention.this maybe the bug of build script of boost.

Upvotes: 2

Related Questions