Home c++ async errors when binding object functions
Reply: 1

c++ async errors when binding object functions

desword Published in 2018-01-12 07:26:10Z

Recently, I encoutered the c++ async error when binding object functions. Normal functions are tested ok when using async. However, when I try to bind the function using class as a parameter, errors are coming. Following is the code:

the class_asy.h


#include <future> 

class A
  A(int i_id):id(i_id) {}

  int id;


class B
  B(int i_id):id(i_id) {}

  int id;


class C

  // a non-optimized way of checking for prime numbers:
  void multi_get(A& a, B& b);
  std::future<void> setup(A& a, B& b);


Then, it is the class_asy.c:

// async example
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include "class_asy.h"

using namespace std;

// a non-optimized way of checking for prime numbers:
void C::multi_get(A& a, B& b) {
  cout << "Calculating. Please, wait...\n";
  cout << a.id * b.id << endl;


std::future<void> C::setup(A& a, B& b)
    std::future<void> fut = std::async(launch::deferred, &C::multi_get, this, a,b);
    return fut;

int main ()

  A testa(2);
  B testb(3);
  C testc;
  std::future<void> fut = testc.setup(testa, testb);

  // call is_prime(313222313) asynchronously:
  // std::future<void> fut = std::async (launch::deferred, &C::multi_get, &testc, testa,testb);

  std::cout << "Checking for the result.\n";


  // if (ret) std::cout << "It is prime!\n";
  // else std::cout << "It is not prime.\n";

  return 0;

The whole error message is as follows:

@ubuntu:~/cpp_dynamic_invoke_success/stdasync_class_test$ bash compi.sh 
In file included from /usr/include/c++/4.8/future:38:0,
                 from class_asy.cpp:3:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (C::*)(A&, B&)>(C*, A, B)>’:
/usr/include/c++/4.8/future:1539:70:   required from ‘std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = void (C::*)(A&, B&); _Args = {C* const, A&, B&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = void]’
class_asy.cpp:19:82:   required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (C::*)(A&, B&)>(C*, A, B)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (C::*)(A&, B&)>(C*, A, B)>’

I have found a way to deal with this problem that just modifying the referred class type into the directly passed class type, specifically.

void multi_get(A& a, B& b); -> void multi_get(A a, B b);

However, it is not an efficient way because the whole object is just copied into the function. I wonder whether there is any other way to fix this error, without modifying the parameter type of referred class.

Some programmer dude
Some programmer dude Reply to 2018-01-12 07:29:44Z

The problem is that you can't pass references as arguments to functions with std::asynch (or using std::thread or std::bind and probably more). You have to use std::ref to wrap reference arguments:

std::async(launch::deferred, &C::multi_get, this, std::ref(a), std::ref(b));

Considering that the called function doesn't modify the arguments passed, it should probably use const references, in which case std::cref has to be used.

You could also pass by value instead, and use move semantics if the actual structures are expensive to copy.

Lastly, it doesn't really make much sense to have a deferred future for something that doesn't produce a result, and only prints something.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306236 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO