如何修复 C++11 中 std::chrono 比较的编译错误?

How to fix this compile error for std::chrono comparison in C++11?

我正在按照示例 ASIO server with timeout 进行操作,此处显示的函数行已从 deadline_timer::traits_type::now() 修改为 std::chrono::steady_clock::now(),因为我想使用独立的 ASIO 而无需促进。 ASIO 可以独立使用 C++11。

void check_deadline(deadline_timer* deadline)
{
    if (stopped())
      return;

    // Check whether the deadline has passed. compare the deadline against
    // the current time 
    // I modified this to be std::chrono::steady_clock::now()
    if (deadline->expires_at() <= deadline_timer::traits_type::now())         {
      // deadline is asio::high_resolution_time type
      stop();
    } else {
      // Put the actor back to sleep.
      deadline->async_wait(
          std::bind(&TCP_Session::check_deadline, shared_from_this(), deadline));    
    }
  }

问题

在 VS2015 中,编译工作正常,但在 Eclipse G++4.9.2 中,它会报错

no match for 'operator<=' (operand types are 
'asio::basic_waitable_timer<std::chrono::_V2::system_clock>::time_point 

aka 
std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long int, std::ratio<1ll, 1000000000ll> > >}'

 and 

'std::chrono::_V2::steady_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long long int, std::ratio<1ll, 1000000000ll> > >}')  TCPSession.h    line 284    C/C++ Problem

问题:

我发现我只能用C++11,不能用C++14。那么如何在 C++11 中解决这个问题(没有提升,或者经典的 unix C)?

Boost Asio 是否使用 std::chrono 取决于预处理器定义(或者定义取决于检测到的编译器,我不记得了)。

我记得,没有这种依赖性的计时器将是 asio::high_resolution_timer。只要确保您在那里使用正确的时钟和计时器组合即可。

在我看来你也应该验证名为 deadline 的变量的实际类型。

不幸的是standalone ASIO hasn't defined a deadline timer to match boost ASIO。但是,我认为您会发现 asio::steady_timer 应该有效。

我使用以下宏在 'standalone' 和 'boost' 之间切换:

#ifdef ASIO_STANDALONE
  #include <asio.hpp>
  #define ASIO asio
  #define ASIO_ERROR_CODE asio::error_code
  #define ASIO_TIMER asio::steady_timer
#else
  #include <boost/asio.hpp>
  #define ASIO boost::asio
  #define ASIO_ERROR_CODE boost::system::error_code
  #define ASIO_TIMER boost::asio::deadline_timer
#endif