【问题标题】:How to fix this compile error for std::chrono comparison in C++11?如何修复 C++11 中 std::chrono 比较的编译错误?
【发布时间】:2016-01-21 00:01:22
【问题描述】:

我按照示例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 中解决这个问题(没有 boost,或者经典的 unix C)?

【问题讨论】:

  • 嗯,其中一个来自system_clock,另一个来自steady_clock。它们是不同的时钟。

标签: c++ c++11 boost-asio chrono


【解决方案1】:

很遗憾,standalone ASIO 没有定义 deadline timer 来匹配 boost ASIO。但是,我认为您会发现 asio::steady_timer 应该可以工作。

我使用以下宏在“独立”和“增强”之间切换:

#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

【讨论】:

【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2013-12-07
    • 1970-01-01
    • 2017-08-31
    • 2012-08-21
    • 2019-11-07
    • 2013-11-12
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多