What is std::future in C++?

In C++, the std::future is a class template used to receive the result of an asynchronously executed task that is not computed yet i.e. future value. Asynchronous operations in C++ are managed using std::async, std::packaged_task, or std::promise, which returns a std::future object. We can use this std::future object to verify, await, or obtain the outcome of the operation.

It is to be noted that we can receive the result of the computation only once in the program as the state of the future is reset after getting the result.

Syntax of std::future

The syntax to define a std::future object is pretty straightforward:

std::future<type> name;

where,

  • name: name of the future object.
  • type: type of the data that is to be recieved.

std::future in C++

The C++ Concurrency support library includes the std::future class template, which has been available since C++11. This template is defined in the <future> header and provides a means to access the outcomes of asynchronous operations.

Similar Reads

What is std::future in C++?

In C++, the std::future is a class template used to receive the result of an asynchronously executed task that is not computed yet i.e. future value. Asynchronous operations in C++ are managed using std::async, std::packaged_task, or std::promise, which returns a std::future object. We can use this std::future object to verify, await, or obtain the outcome of the operation....

Member Functions in std::future Class

The std::future class consists of the various member functions providing basic functionality. Some of the common ones are:...

Examples of std::future in C++

Example 1: Using std::future to Print the Value Returned by Asynchronous Task...

Conclusion

...