简体   繁体   中英

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 . I would recommend to keep using just std::chrono since it is more type-safe than it's counterpart.

#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! And there is no operator >= to compare. Then you try to compare nanosecond with second
The time_point has a function named time_since_epoch and you can use it.
Using auto can help solve the problem but not understanding what happens and what is under the hood!
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


  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM