c++c++20stdasync

std::async on c++20 no matching function


I have the following code snippets and when compiling with c++20, it is failing with the following error msg, no matching function for call to async and would not yield a useful error message. Proper header files are included.

driver.cpp:40:28: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, MarketDataProcessor*, std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > (MarketDataProcessor::*)(uint16_t, uint8_t), int, int)’
   40 |     std::cout << std::async(std::launch::async, &MarketDataProcessor::onCallback, &mdproc, &MarketDataProcessor::on_packet, 1, 3).get();
      |                  ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from driver.cpp:6:
/usr/include/c++/11/future:1757:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(std::launch, _Fn&&, _Args&& ...)’
 1757 |     async(launch __policy, _Fn&& __fn, _Args&&... __args)
      |     ^~~~~
/usr/include/c++/11/future:1757:5: note:   template argument deduction/substitution failed:
driver.cpp:40:28: note:   couldn’t deduce template parameter ‘_Fn’
   40 |     std::cout << std::async(std::launch::async, &MarketDataProcessor::onCallback, &mdproc, &MarketDataProcessor::on_packet, 1, 3).get();
      |                  ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from driver.cpp:6:
/usr/include/c++/11/future:1791:5: note: candidate: ‘template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(_Fn&&, _Args&& ...)’
 1791 |     async(_Fn&& __fn, _Args&&... __args)
      |     ^~~~~
/usr/include/c++/11/future:1791:5: note:   template argument deduction/substitution failed:
/usr/include/c++/11/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::__invoke_result<typename std::decay<_Tp>::type, typename std::decay<_Args>::type ...>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]’:
driver.cpp:40:28:   required from here
/usr/include/c++/11/future:1791:5: error: no type named ‘type’ in ‘struct std::__invoke_result<std::launch>’
      |                  

The code snippets according the above error message.

In market data processor header file and in MarketDataProcessor class

 49 decltype(auto) onCallback(std::function<std::pair<std::string, std::string>(uint16_t, uint8_t)> f, uint16_t seq_num,     uint8_t num_msgs)
 50          {
 51              std::lock_guard<std::mutex> lk_redLogic(RedundancyMgr::redMgr_mutex);
 52              RedundancyMgr::addPacket(Red_Path::Path1, seq_num, num_msgs);
 53              return RedundancyMgr::redundancy_logic([&](bool shouldRun, uint16_t seq_no, uint8_t num_msg) {
 54                  if(shouldRun) {
 55                      std::cout << std::boolalpha << "shouldRun: " << shouldRun /*<< " on path: " << (int)path*/ << std::endl;
 56                      return f(seq_num, num_msgs);
 57                  }
 58 
 59                  std::cout << "dropping duplicate seq_no: " << seq_no <<" num_msg: " << (unsigned int)num_msg
 60                            /*<< " on path: " << (int)path*/ << std::endl;
 61                  return std::make_pair(std::string(""), std::string(""));
 62              });
 63          
 64          }

The onCallback function takes a callback function f with the signature specified above and parameters seq_num and num_msgs.

driver.cpp uses async to run it asynchronously and is used as follows from the client side
39     MarketDataProcessor mdproc;
40     std::cout << std::async(std::launch::async, &MarketDataProcessor::onCallback, &mdproc, &MarketDataProcessor::on_packet, 1, 3).get();

From the client side, onCallback is called asynchronously passing the object pointer, another member function to be called and the numeric arguments.

But for some reason, async is failing to compile with the error message posted above. c++20 is used to compile it.

The second function on_packet should not be static, as making it static would require a number of modifications of the on_packet function. It is simple to reproduce it by defining the above function and trying to call it from the main function. Thank you in advance!

Any idea?

Tried to build it and is failing, may be an issue with the compiler.


Solution

  • The issue is MarketDataProcessor::onCallback is overloaded, and the compiler can't figure out which overload you're referring to in the call to std::async. I would recommend calling std::async with a lambda function to resolve this problem:

    std::cout << std::asycnc(std::launch::async, []{
        MarketDataProcessor mdproc;
        mdProc.onCallback(&MarketDataProcessor::on_packet, 1, 3); 
    }).get();