此代码在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.
最佳答案
好吧,我明白了。
在gcc stl中,system_clock的持续时间为纳秒,但在clang中为微秒。
关于c++ - 从time_point +持续时间到持续时间的转换在Clang上失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22205290/