binary'> =':'std :: chrono :: system_clock :: time_point'

时间:2017-02-23 08:54:24

标签: c++ c++11 visual-studio-2015 chrono

我不知道如何驾驭这个编译错误:

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)
  {

  }
}

以下是编译器输出的内容:

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 ==========

2 个答案:

答案 0 :(得分:5)

我建议你在这种情况下使用自动类型推导,因为它使代码更清晰。另外,如上面的评论中所述, std :: chrono 工具与 c-style time_t 不直接兼容。我建议继续使用 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;
}

答案 1 :(得分:1)

您尝试比较 C ++ time_point和C时间!而且没有运算符>=可供比较。然后,您尝试将nanosecondsecond进行比较 time_point 有一个名为time_since_epoch的函数,您可以使用它 使用auto可以帮助解决问题,但不能理解发生了什么以及引擎盖下的内容! 所以你只需比较(不好):
if ( now.time_since_epoch().count() >= now_c)

更好代码是:
std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count()

因为time_tsecond

  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';
  }  

输出:

1487879248873636085
1487879248
1487792848