Convert time_point to a specific duration using a chronograph

/*definition of start and end
std::chrono::time_point<std::chrono::system_clock> start;
std::chrono::time_point<std::chrono::system_clock> _end;
*/

std::chrono::time_point<std::chrono::system_clock> someclass::spf()
{
    _end = std::chrono::system_clock::now();
    std::chrono::time_point<std::chrono::system_clock> time(_end-start);
    start = std::chrono::system_clock::now();
    return time;
}
unsigned int someclass::secs()
{
    return std::chrono::duration_cast<std::chrono::seconds>(spf()).count();
}

The compiler gives me errors to call duration_cast. Exact error:

error: no matching function for call to β€˜duration_cast(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >)’
+4
source share
1 answer

A temporary point ( std::chrono::time_point<...>) is not a duration ( std::chrono::duration<...>e.g. std::chrono::seconds, which is a typedef).

These are different types.

You are trying to specify a duration, but spf returns time_point. Duration is the time between 2 time_point. So, to get the duration from time_point, you need another time_point and get the duration between these time_points.

, , spf time_point , :

std::chrono::time_point<std::chrono::system_clock> time(_end-start);

, , time_point. :

const auto time_since_start = _end - start;
//...
return time_since_start; // you might need a duration cast here depending on what spf() will return.

, spf() , , , .

+10

Source: https://habr.com/ru/post/1525705/


All Articles