4


I'm kind of new to std::async and I have problems understanding how it works. I saw example of how to pass member funcion to std::async somewhere on the web but it seems not to compile. Whats the problem here? How can it be done without lambda?

class Test {
  std::future<void> f;
  void test() {}
  void runTest() {
    // ERROR no instance overloaded matches argument list
    f = std::async(std::launch::async, &Test::test, this); 

    // OK...
    f = std::async(std::launch::async, [this] {test();}); 
  }
  void testRes() {
    f.get();
  }
};
4
  • is void Test::test() an overloaded member function? Commented Feb 18, 2018 at 23:22
  • No, it is not. Tried to keep minimal reproducible example as close as it is in original source :) Commented Feb 18, 2018 at 23:27
  • What compiler and version? There's nothing wrong with the code you've shown. Please include headers, main() etc so it's a minimal reproducible example. Commented Feb 18, 2018 at 23:34
  • The information provided here is not sufficient to help you, at best we can only make educated guesses. please post a MCVE Commented Feb 18, 2018 at 23:34

1 Answer 1

2

You should be able to use std::bind to create a callable function object:

f = std::async(std::launch::async, std::bind(&Test::test, this));

In your example &Test::test is not a function that takes one parameter of type Test, as it would be expected by std::async. Instead it is member function, which must be called differently.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.