gpt4 book ai didi

c++ - 从 time_point + duration 到 duration 的转换在 clang 上失败

转载 作者:行者123 更新时间:2023-11-28 07:02:41 27 4
gpt4 key购买 nike

此代码在 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 + duration 到 duration 的转换在 clang 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com