mercredi 19 juin 2019

error: conversion from std::chrono::time_point float to non-scalar type long int requested

I have this code that tries to create a new time_point by adding a duration:

// now
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// duration
std::chrono::duration<float> duration(1.0f / 60.0f); // float in seconds
// now + duration
std::chrono::system_clock::time_point futureTime = now + duration;

But it gives me this error

error: conversion from 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<float, std::ratio<1, 1000000000> > >' to non-scalar type '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> > >}' requested
# it's over there in the distance --->

# ... again, with some manual formatting:
error:
    conversion from
    'std::chrono::time_point<std::chrono::_V2::system_clock,
                             std::chrono::duration<float, 
                                                   std::ratio<1, 1000000000> > >'
    to non-scalar type
    '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> > >}'
    requested

After some heavy squinting there's a float in the first one and a long int in the second. Thus now (long int) + duration (float) gives a time_point with some internal floating point duration and I'm presumably asking for it to be stuffed back into the default long int representation.

I'd like to eventually pass this time point to std::condition_variable::wait_until. How can I force the conversion to a std::chrono::system_clock::time_point?

What kind of precision will I lose if I do (i.e. is it storing milliseconds in which case I'll lose some of my 1 / 60)?

If I don't convert it, what's a nice short way of writing the type that now + duration returns (yes I could use auto but for the sake of say passing it to a function)?

Aucun commentaire:

Enregistrer un commentaire