#include <chrono> with Clang 3.1

I am using Clang 3.1 on Ubuntu. Compiling this simple program:

#include <thread>
int main(int argc, char ** argv)
{
retrurn 0;
}

using the following command line fails:
clang++ -std=c++11 - test.cpp

The failure is due to the inclusion of <chrono> from <thread>. Replacing the
#include <thread>
with
#include <chrono>
gives the same results.

Is there a way to work with std::thread using Clang?

Thanks

Ofer

<thread> needs <chrono> to support these two functions:

template <class Clock, class Duration>
void sleep_until(const chrono::time_point<Clock, Duration>& abs_time);

template <class Rep, class Period>
void sleep_for(const chrono::duration<Rep, Period>& rel_time);

What is the error you're seeing?

Howard

The errors are:
/usr/include/c++/4.6/chrono:240:10: error: cannot cast from lvalue of type 'const long long' to rvalue reference type 'rep' (aka 'long long &&'); types are not compatible
          : __r(static_cast<rep>(__rep)) { }
                ^~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:128:13: note: in instantiation of function template specialization 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::duration<long
      long, void>' requested here
            return _ToDur(static_cast<__to_rep>(__d.count()));
                   ^
/usr/include/c++/4.6/chrono:182:9: note: in instantiation of function template specialization 'std::chrono::__duration_cast_impl<std::chrono::duration<long long &&,
      std::ratio<1, 1000000> >, std::ratio<1, 1>, long long &&, true, true>::__cast<long long, std::ratio<1, 1000000> >' requested here
        return __dc::__cast(__d);
               ^
/usr/include/c++/4.6/chrono:247:10: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long long &&, std::ratio<1,
      1000000> >, long long, std::ratio<1, 1000000> >' requested here
          : __r(duration_cast<duration>(__d).count()) { }
                ^
/usr/include/c++/4.6/chrono:466:9: note: in instantiation of function template specialization 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::duration<long
      long, std::ratio<1, 1000000>, void>' requested here
        return __ct(__lhs).count() < __ct(__rhs).count();
               ^
/usr/include/c++/4.6/chrono:667:7: note: in instantiation of function template specialization 'std::chrono::operator<<long long, std::ratio<1, 1000000>, long long,
      std::ratio<1, 1000000> >' requested here
                    < system_clock::duration::zero(),
                    ^
/usr/include/c++/4.6/chrono:128:13: error: call to implicitly-deleted copy constructor of 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >'
            return _ToDur(static_cast<__to_rep>(__d.count()));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:182:9: note: in instantiation of function template specialization 'std::chrono::__duration_cast_impl<std::chrono::duration<long long &&,
      std::ratio<1, 1000000> >, std::ratio<1, 1>, long long &&, true, true>::__cast<long long, std::ratio<1, 1000000> >' requested here
        return __dc::__cast(__d);
               ^
/usr/include/c++/4.6/chrono:247:10: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long long &&, std::ratio<1,
      1000000> >, long long, std::ratio<1, 1000000> >' requested here
          : __r(duration_cast<duration>(__d).count()) { }
                ^
/usr/include/c++/4.6/chrono:466:9: note: in instantiation of function template specialization 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::duration<long
      long, std::ratio<1, 1000000>, void>' requested here
        return __ct(__lhs).count() < __ct(__rhs).count();
               ^
/usr/include/c++/4.6/chrono:667:7: note: in instantiation of function template specialization 'std::chrono::operator<<long long, std::ratio<1, 1000000>, long long,
      std::ratio<1, 1000000> >' requested here
                    < system_clock::duration::zero(),
                    ^
/usr/include/c++/4.6/chrono:233:12: note: explicitly defaulted function was implicitly deleted here
        constexpr duration(const duration&) = default;
                  ^
/usr/include/c++/4.6/chrono:349:6: note: copy constructor of 'duration<long long &&, std::ratio<1, 1000000> >' is implicitly deleted because field '__r' is of rvalue
      reference type 'rep' (aka 'long long &&')
        rep __r;
            ^
/usr/include/c++/4.6/chrono:182:9: error: call to implicitly-deleted copy constructor of 'typename enable_if<__is_duration<duration<long long &&, ratio<1, 1000000> >
      >::value, duration<long long &&, ratio<1, 1000000> > >::type' (aka 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >')
        return __dc::__cast(__d);
               ^~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:247:10: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long long &&, std::ratio<1,
      1000000> >, long long, std::ratio<1, 1000000> >' requested here
          : __r(duration_cast<duration>(__d).count()) { }
                ^
/usr/include/c++/4.6/chrono:466:9: note: in instantiation of function template specialization 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::duration<long
      long, std::ratio<1, 1000000>, void>' requested here
        return __ct(__lhs).count() < __ct(__rhs).count();
               ^
/usr/include/c++/4.6/chrono:667:7: note: in instantiation of function template specialization 'std::chrono::operator<<long long, std::ratio<1, 1000000>, long long,
      std::ratio<1, 1000000> >' requested here
                    < system_clock::duration::zero(),
                    ^
/usr/include/c++/4.6/chrono:233:12: note: explicitly defaulted function was implicitly deleted here
        constexpr duration(const duration&) = default;
                  ^
/usr/include/c++/4.6/chrono:349:6: note: copy constructor of 'duration<long long &&, std::ratio<1, 1000000> >' is implicitly deleted because field '__r' is of rvalue
      reference type 'rep' (aka 'long long &&')
        rep __r;
            ^
/usr/include/c++/4.6/chrono:255:11: error: rvalue reference to type 'long long' cannot bind to lvalue of type 'long long'
        { return __r; }
                 ^~~
/usr/include/c++/4.6/chrono:247:39: note: in instantiation of member function 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::count' requested here
          : __r(duration_cast<duration>(__d).count()) { }
                                             ^
/usr/include/c++/4.6/chrono:466:9: note: in instantiation of function template specialization 'std::chrono::duration<long long &&, std::ratio<1, 1000000> >::duration<long
      long, std::ratio<1, 1000000>, void>' requested here
        return __ct(__lhs).count() < __ct(__rhs).count();
               ^
/usr/include/c++/4.6/chrono:667:7: note: in instantiation of function template specialization 'std::chrono::operator<<long long, std::ratio<1, 1000000>, long long,
      std::ratio<1, 1000000> >' requested here
                    < system_clock::duration::zero(),
                    ^
/usr/include/c++/4.6/chrono:666:21: error: static_assert expression is not an integral constant expression
      static_assert(system_clock::duration::min()
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:666:21: note: undefined function 'operator<<long long, std::ratio<1, 1000000>, long long, std::ratio<1, 1000000> >' cannot be used in a constant
      expression
/usr/include/c++/4.6/chrono:460:7: note: declared here
      operator<(const duration<_Rep1, _Period1>& __lhs,
      ^
/usr/include/c++/4.6/chrono:141:40: error: cannot cast from lvalue of type 'const intmax_t' (aka 'const long long') to rvalue reference type 'long long &&'; types are not
      compatible
              static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:182:9: note: in instantiation of function template specialization 'std::chrono::__duration_cast_impl<std::chrono::duration<long long,
      std::ratio<1, 1> >, std::ratio<1, 1000000>, long long &&, true, false>::__cast<long long, std::ratio<1, 1000000> >' requested here
        return __dc::__cast(__d);
               ^
/usr/include/c++/4.6/chrono:679:21: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long long, std::ratio<1, 1>
      >, long long, std::ratio<1, 1000000> >' requested here
        return std::time_t(duration_cast<chrono::seconds>
                           ^
/usr/include/c++/4.6/chrono:154:40: error: cannot cast from lvalue of type 'const intmax_t' (aka 'const long long') to rvalue reference type 'long long &&'; types are not
      compatible
              static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.6/chrono:182:9: note: in instantiation of function template specialization 'std::chrono::__duration_cast_impl<std::chrono::duration<long long,
      std::ratio<1, 1000000> >, std::ratio<1000000, 1>, long long &&, false, true>::__cast<long long, std::ratio<1, 1> >' requested here
        return __dc::__cast(__d);
               ^
/usr/include/c++/4.6/chrono:577:22: note: in instantiation of function template specialization 'std::chrono::duration_cast<std::chrono::duration<long long, std::ratio<1,
      1000000> >, long long, std::ratio<1, 1> >' requested here
        return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
                            ^
/usr/include/c++/4.6/chrono:687:9: note: in instantiation of function template specialization 'std::chrono::time_point_cast<std::chrono::duration<long long, std::ratio<1,
      1000000> >, std::chrono::system_clock, std::chrono::duration<long long, std::ratio<1, 1> > >' requested here
        return time_point_cast<system_clock::duration>
               ^
7 errors generated.

The errors are:
/usr/include/c++/4.6/chrono:240:10: error: cannot cast from lvalue of type 'const long long' to rvalue reference type 'rep' (aka 'long long &&'); types are not compatible
: __r(static_cast<rep>(__rep)) { }
^~~~~~~~~~~~~~~~~~~~~~~

[snip]

You are using libstdc++ as your standard library. It needs to be
patched [1] to work with clang. Otherwise you can use libc++ [2]
as your standard library which works out of the box with clang.

Note that the patch I linked to is against libstdc++ from gcc 4.7.
It may or may not work with your libstdc++, which appears to be
from gcc 4.6 (at the least you may have to apply the patch
manually, which isn't a big deal as it's just one line).

Regards,
Nate

[1] http://clang.llvm.org/libstdc++4.7-clang11.patch
[2] http://libcxx.llvm.org/

Does adding -stdlib=libc++ to your command line help?

Howard