[英]binary '>=': 'std::chrono::system_clock::time_point'
I am not sure how to get ride of this compiler error: 我不确定如何解决此编译器错误:
error C2676: binary '>=': 'std::chrono::system_clock::time_point'
#include <ctime>
#include <chrono>
int main()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
if (std::chrono::system_clock::now() >= now_c)
{
}
}
Here is what the compiler outputs: 这是编译器输出的内容:
1>------ Build started: Project: test, Configuration: Debug x64 ------
1> Source.cpp
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility(311): note: see declaration of 'std::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::duration<_Rep,_Period> &,const std::chrono::duration<_Rep2,_Period2> &)': could not deduce template argument for 'const std::chrono::duration<_Rep,_Period> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(538): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::time_point<_Clock,_Duration> &,const std::chrono::time_point<_Clock,_Duration2> &)': could not deduce template argument for 'const std::chrono::time_point<_Clock,_Duration2> &' from 'time_t'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(905): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2676: binary '>=': 'std::chrono::system_clock::time_point' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I recommend you to use type deduction with auto for this kind of case, as it makes the code much clearer. 对于这种情况,我建议您将类型归纳与自动配合使用,因为这样会使代码更清晰。 Also, as said in the comments above, the std::chrono facilities are not directly compatible with c-style time_t .
同样,如上面的评论所述, std :: chrono工具与c风格的time_t不直接兼容。 I would recommend to keep using just std::chrono since it is more type-safe than it's counterpart.
我建议继续使用std :: chrono,因为它比对应的类型更安全。
#include <ctime>
#include <chrono>
#include <iostream>
int main()
{
auto now = std::chrono::system_clock::now();
auto now_c = now - std::chrono::hours(24);
if (std::chrono::system_clock::now() >= now_c)
{
std::cout << "it works!" << std::endl;
}
return 0;
}
You are try to compare a C++ time_point with a C time! 您正在尝试将 C ++ time_point与C时间进行比较 ! And there is no operator
>=
to compare. 并且没有运算符
>=
进行比较。 Then you try to compare nanosecond
with second
然后,您尝试将
nanosecond
与second
进行比较
The time_point has a function named time_since_epoch
and you can use it. time_point具有一个名为
time_since_epoch
的函数,您可以使用它。
Using auto
can help solve the problem but not understanding what happens and what is under the hood! 使用
auto
可以帮助解决问题,但不能理解会发生什么以及实际情况是什么!
So you simply can compare( not good ): 所以你可以简单地比较( 不好 ):
if ( now.time_since_epoch().count() >= now_c)
And the better code is: 更好的代码是:
std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count()
Because time_t
is per second
因为
time_t
是每second
if ( now.time_since_epoch().count() >= now_c){
std::cout << now.time_since_epoch().count() << '\n';
std::cout << std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count() << '\n';
std::cout << now_c << '\n';
}
the output: 输出:
1487879248873636085
1487879248
1487792848
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.