1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// future::get
#include <iostream> // std::cout, std::ios
#include <future> // std::async, std::future
#include <exception> // std::exception
int get_int() {
std::cin.exceptions (std::ios::failbit); // throw on failbit set
int x;
std::cin >> x; // sets failbit if invalid
return x;
}
int main ()
{
std::future<int> fut = std::async (get_int);
std::cout << "Please, enter an integer value: ";
try {
int x = fut.get();
std::cout << "You entered: " << x << '\n';
}
catch (std::exception&) {
std::cout << "[exception caught]";
}
return 0;
}
| |