我有一个包含std::chrono::system_clock::time_point
struct NetInfo {
std::chrono::system_clock::time_point time;
std::chrono::steady_clock::time_point start;
};
当我尝试将其中一个time_point分配给std::chrono::system_clock::now()
api::NetInfo ni;
ni.start = std::chrono::system_clock::now();
我收到了这个巨大的错误
/home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/src/NetService.cpp:48:51: error: no match for ‘operator=’ (operand types are ‘std::chrono::_V2::steady_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}’ and ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}’)
ni.start = std::chrono::system_clock::now();
^
In file included from /usr/include/c++/7.2.1/thread:38:0,
from /home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/./include/NetService.h:3,
from /home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/src/NetService.cpp:1:
/usr/include/c++/7.2.1/chrono:610:14: note: candidate: constexpr std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >& std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >::operator=(const std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&)
struct time_point
^~~~~~~~~~
/usr/include/c++/7.2.1/chrono:610:14: note: no known conversion for argument 1 from ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}’ to ‘const std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&’
/usr/include/c++/7.2.1/chrono:610:14: note: candidate: constexpr std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >& std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >::operator=(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&&)
/usr/include/c++/7.2.1/chrono:610:14: note: no known conversion for argument 1 from ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}’ to ‘std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&&’
关于我在这里做错了什么的想法?
答案 0 :(得分:4)
虽然未指定,但它是a
正在衡量Unix Time的事实上的标准(自1970-01-01 00:00:00 UTC以来大约持续时间),但具有不同的精确度每个平台。
system_clock
的时代也未具体说明,并没有发展出事实上的标准。不同的平台做不同的事情。例如,在我的平台上steady_clock
测量自计算机启动以来的时间。
steady_clock
旨在在编译时捕获尽可能多的逻辑错误。将<chrono>
分配给system_clock::time_point
将是一个逻辑错误,因为这两个时钟针对不同的时期进行测量。
未来的标准可能会提供一种方法将一个时钟steady_clock::time_point
“转换”或“转换”到另一个时钟,但这不是一个简单的任务。它必须涉及一些计算,其中考虑了时期的差异。否则,time_point
将代表分配后与分配前相比的不同时刻。
tl; dr:编译器在编译时遇到了逻辑错误。