Search code examples
c++classstd-future

How to instantiate public members of a class and return it as a std::promise?


I wish to instantiate public members of a class and return it as a promise.
This is what I am trying to do:

class A
{
public:
   int x;
};

std::future<A> returnPromiseA(int y)
{
   std::promise<A> promise;
   promise.x = y; //<- THIS IS INCORECT
   return promise;
}

promise.x = y; is an incorrect syntax.
What is the correct syntax for assignment?


Solution

  • Current C++ async classes cannot chain continuations so you'll have to do it other way:

    std::future<A> returnFutureA(int y) {
        return std::async([y]{ A retVal; retVal.x = y; return retVal; });
    }