繁体   English   中英

当我编译c ++ 11的chrono time时,它报告错误

[英]When I compile chrono time of c++11, It report error

代码最像

  uint64_t nanoseconds = 0;
  auto nano = std::chrono::nanoseconds(nanoseconds_);
  system_clock::time_point tp(nano);

报告错误(QNX):

time.cpp:86:35: error: no matching function for call to 'std::__1::chrono::time_point<std::__1::chrono::system_clock>::time_point(std::__1::chrono::duration<long long int, std::__1::ratio<1l, 1000000000l> >&) 
qnx700/target/qnx7/usr/include/c++/v1/chrono:769:5: note: candidate: 
template<class _Duration2> std::__1::chrono::time_point<_Clock, _Duration>::time_point(const std::__1::chrono::time_point<_Clock, _Duration2>&,
typename std::__1::enable_if<std::__1::is_convertible<_Duration2, 
_Duration>::value>::type*) time_point(const time_point<clock, _Duration2>& t,
^ qnx700/target/qnx7/usr/include/c++/v1/chrono:769:5: note: template argument 
deduction/substitution failed: time.cpp:86:35: note: 
'std::__1::chrono::duration<long long int, std::__1::ratio<1l, 1000000000l> >'
 is not derived from 'const 
 std::__1::chrono::time_point<std::__1::chrono::system_clock, _Duration2>''

如何将std::chrono::nanoseconds time_point std::chrono::nanoseconds time_pointtime_point

谢谢大家

std::chrono::time_point 没有使用任意持续时间类型作为模板参数的构造函数。 采用system_clock::time_point::duration构造函数构造一个时间点,并将其time_since_epoch()属性设置为指定的参数,即,构造的时间点表示的时间是其“纪元”之后的指定持续时间。

如果这是您的意图,则可以使用duration_cast将纳秒值转换为system_clock::time_point::duration

using namespace std::chrono;

nanoseconds nano(1234567);

system_clock::time_point time(
    duration_cast<system_clock::duration>(nano));

或者,您可以构造一个持续时间为零(即代表纪元的时间点)的system_clock::time_point ,然后向其添加纳秒值; 结果是一个新的时间点:

system_clock::time_point epoch;
system_clock::time_point time = epoch + nano;

正如Howard Hinnant所暗示的那样,该标准不保证std::chrono::system_clock::duration任何特定精度,即系统时钟可能无法测量小至纳秒的间隔。 在我的系统(Linux x86_64,gcc 7.3 / glibc 2.20)上,持续时间精度为1纳秒,但在其他平台上,则可能为1微秒或更短。 在这样的平台上, duration_cast<system_clock::duration>(nanoseconds(1))的结果为零,因为duration_cast朝四舍五入

(还要注意,即使精度是1纳秒, 精度也可能不是!这是另一个话题。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM