operator =两个std :: chrono :: time_point导致错误

时间:2018-02-05 17:21:53

标签: c++ chrono

我有一个包含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> > >&&’

关于我在这里做错了什么的想法?

1 个答案:

答案 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:编译器在编译时遇到了逻辑错误。