1

此代码在 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.
4

1 回答 1

3

好的,我明白了。

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

于 2014-03-05T18:01:04.317 回答