[英]converting time_point to specific duration with chrono
/*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();
}
編譯器給出了duration_cast
調用的錯誤。 確切的錯誤:
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> > >)’
時間點( std::chrono::time_point<...>
)不是持續時間( std::chrono::duration<...>
,例如std::chrono::seconds
,例如typedef) 。
它們是不同的類型。
您正在嘗試轉換持續時間,但spf返回time_point。 持續時間是2個時間點之間的時間。 因此,要從time_point獲取持續時間,您需要另一個time_point並獲取這些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()的返回類型必須是持續時間,如秒或毫秒或任何你想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.