簡體   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