0

How can I pass class method to std::async?

Example:

#include <future>
#include <iostream>

class ExampleClass { public: 
    void exampleMethod() {
        std::cout << "foo\n";
    }

    auto initializeExampleMethod() {
        return std::async(std::launch::async, &this->handler, &this);
    }
}

In Visual Studio Code (on Windows) it shows error a pointer to a bound function may only be used to call the function. How can I solve this easily?

maDeveloper
  • 84
  • 11
  • The "similar question" also doesn't works. – maDeveloper Mar 12 '21 at 10:00
  • 1
    "doesn't works" is not a problem description! Why not? What code did you write? What error did you get? [Edit] your question to show everything you've tried and the specific problems encountered. – underscore_d Mar 12 '21 at 10:01
  • Pls read the dupe carefully. It has the syntax `&Person::sum`, which in your case would be `&ExampleClass::exampleMethod`. `&this->handler` is not the right syntax for pointer-to-member. Also change `&this` to `this`. – rustyx Mar 12 '21 at 10:26

0 Answers0