我正在尝试从两个时间duration
计算一个时间time_points
,如此
#include <chrono>
using namespace std;
using namespace std::chrono;
int main() {
time_point<high_resolution_clock> start = high_resolution_clock::now();
time_point<high_resolution_clock> end = high_resolution_clock::now();
duration<milli> difference = duration_cast<milli>(end - start);
return 0;
}
我对编译器错误消息感到十分困惑:
Compilation error time: 0 memory: 0 signal:0
prog.cpp: In function 'int main()':
prog.cpp:9:63: error: no matching function for call to 'duration_cast(std::__success_type<std::chrono::duration<long long int, std::ratio<1ll, 1000000000ll> > >::type)'
duration<milli> difference = duration_cast<milli>(end - start);
^
prog.cpp:9:63: note: candidate is:
In file included from prog.cpp:2:0:
/usr/include/c++/4.9/chrono:194:7: note: template<class _ToDur, class _Rep, class _Period> constexpr typename std::enable_if<std::chrono::__is_duration<_Tp>::value, _ToDur>::type std::chrono::duration_cast(const std::chrono::duration<_Rep, _Period>&)
duration_cast(const duration<_Rep, _Period>& __d)
^
/usr/include/c++/4.9/chrono:194:7: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/chrono: In substitution of 'template<class _ToDur, class _Rep, class _Period> constexpr typename std::enable_if<std::chrono::__is_duration<_Tp>::value, _ToDur>::type std::chrono::duration_cast(const std::chrono::duration<_Rep, _Period>&) [with _ToDur = std::ratio<1ll, 1000ll>; _Rep = <missing>; _Period = <missing>]':
prog.cpp:9:63: required from here
/usr/include/c++/4.9/chrono:194:7: error: no type named 'type' in 'struct std::enable_if<false, std::ratio<1ll, 1000ll> >'
两个time_point
之间的差异不是持续时间吗?我是否可以不在持续时间类型之间转换以获得毫秒持续时间表示?
答案 0 :(得分:4)
milli
是std::ratio<1, 1000>
的typedef,它不是duration
的有效表示,应该类似于duration<long, milli>
,或者只是std::chrono::milliseconds
< / p>
auto difference = duration_cast<milliseconds>(end - start);