简体   繁体   中英

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

The code most like

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

It report error(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>''

How can I combine std::chrono::nanoseconds with time_point

Thanks for all.

std::chrono::time_point does not have a constructor that takes an arbitrary duration type as a template parameter. The constructor that takes system_clock::time_point::duration constructs a time point with its time_since_epoch() property set to the specified argument, ie, the constructed time point represents the time which is the specified duration after its "epoch."

If this is your intent, you can use duration_cast to cast your nanoseconds value to system_clock::time_point::duration :

using namespace std::chrono;

nanoseconds nano(1234567);

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

Or, you can construct a system_clock::time_point with a duration of zero (ie, a time point representing the epoch) and then add your nanoseconds value to this; the result is a new time point:

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

As alluded to by Howard Hinnant, the standard does not guarantee any particular precision for std::chrono::system_clock::duration , ie, the system clock may not be capable of measuring intervals as small as a nanosecond. On my system (Linux x86_64, gcc 7.3 / glibc 2.20) the duration precision is 1 nanosecond, but on other platforms it may be 1 microsecond or less. On such a platform, the result of duration_cast<system_clock::duration>(nanoseconds(1)) is zero, since duration_cast rounds toward zero .

(Note also that even if the precision is 1 nanosecond, the accuracy probably isn't! This is another topic though.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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