繁体   English   中英

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

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

我正在关注带有超时的示例ASIO服务器 ,并且此处显示的函数行已从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)?

不幸的是, 独立ASIO没有定义一个匹配boost ASIOdeadline timer 但是,我想你会发现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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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