从time_point +持续时间到持续时间的转换在clang上失败

时间:2014-03-05 17:50:02

标签: c++ c++11 time clang chrono

此代码在gcc-4.8上编译并在clang-3.3上失败? 以及如何使这段代码在clang上编译? = \

#include <chrono>
#include <iostream>
#include <thread>

void sleep_until_wow(const std::chrono::system_clock::time_point& time)
{
    std::cout << "zzz...\n";
    std::this_thread::sleep_until(time);
    std::cout << "wake up!\n";
} 

template <typename Rep, typename Period>
void sleep_for(const std::chrono::duration<Rep, Period>& duration)
{
    sleep_until_wow(std::chrono::system_clock::now() + duration);
}

template <typename Clock, typename Duration>
void sleep_until(const std::chrono::time_point<Clock, Duration>& time)
{
    sleep_until_wow(time);
}


int main() {
    sleep_for(std::chrono::nanoseconds(500000));
    return 0;
}

错误讯息:

% clang++ -std=c++11 -stdlib=libc++ time_point.cpp 
time_point.cpp:17:5: error: no matching function for call to 'sleep_until_wow'
    sleep_until_wow(std::chrono::system_clock::now() + ns);
    ^~~~~~~~~~~~~~~
time_point.cpp:28:5: note: in instantiation of function template specialization 'sleep_for<long long, std::__1::ratio<1, 1000000000> >' requested here
    sleep_for(std::chrono::nanoseconds(500000));
    ^
time_point.cpp:5:6: note: candidate function not viable: no known conversion from 'time_point<[...], typename common_type<class duration<long long, class ratio<1, 1000000> >, duration<long long, class ratio<1, 1000000000> >
      >::type>' to 'const time_point<[...], (default) typename _Clock::duration>' for 1st argument
void sleep_until_wow(const std::chrono::system_clock::time_point& time)
     ^
1 error generated.

1 个答案:

答案 0 :(得分:1)

好的,我明白了。

在gcc stl中,system_clock持续时间是纳秒,但在clang中它是微秒。