简体   繁体   English

二进制'> =':'std :: chrono :: system_clock :: time_point'

[英]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 然后,您尝试将nanosecondsecond进行比较
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.

相关问题 boost :: serialize和std :: chrono :: system_clock :: time_point - boost::serialize and std::chrono::system_clock::time_point 是 std::atomic <std::optional<std::chrono::time_point<std::chrono::system_clock> &gt;&gt; 有效/安全? </std::optional<std::chrono::time_point<std::chrono::system_clock> - Is std::atomic<std::optional<std::chrono::time_point<std::chrono::system_clock>>> valid/safe? 将std :: chrono :: system_clock :: time_point :: min()转换为字符串时,无效的空指针错误 - Invalid null pointer error when converting std::chrono::system_clock::time_point::min() to string 如何从std :: chrono :: system_clock :: time_point.time_since_epoch()。count()获取std :: chrono :: system_clock :: time_point? - How to get std::chrono::system_clock::time_point from std::chrono::system_clock::time_point.time_since_epoch().count()? C ++将字符串时间戳转换为std :: chrono :: system_clock :: time_point - C++ Convert a string timestamp to std::chrono::system_clock::time_point 如何正确地将字符串转换为 std::chrono::system_clock::time_point? - How to properly convert string to std::chrono::system_clock::time_point? 从持续时间构造 std::chrono::system_clock::time_point - Constructing std::chrono::system_clock::time_point from a duration 将std :: chrono :: system_clock :: time_point转换为struct timeval并返回 - Convert std::chrono::system_clock::time_point to struct timeval and back 将 C# 日期时间转换为 C++ std::chrono::system_clock::time_point - Convert C# DateTime to C++ std::chrono::system_clock::time_point 如何获取 std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> 来自年、月、日、小时、分钟和秒的变量?</std::chrono::system_clock,> - How to get std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> from variables of year,month,day,hour,minute and seconds?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM