-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_future.cpp
47 lines (40 loc) · 1.19 KB
/
optional_future.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <future>
#include <utility>
#include <array>
#include <iostream>
#include <optional>
#include <vector>
using BlockDigest = std::array<std::uint8_t, 32>;
// using BlockDigest = int;
std::optional<BlockDigest> getDigest() {
// BlockDigest opt = 10;
BlockDigest opt = {'a', 'b'};
return std::make_optional<BlockDigest>(opt);
}
std::future<std::optional<BlockDigest>> getFutureForDigest() {
// BlockDigest opt = 10;
BlockDigest opt = {'a', 'b'};
return std::async([opt]() { return std::make_optional<BlockDigest>(opt); });
}
int main() {
std::vector<std::future<std::optional<BlockDigest>>> futures;
for (size_t i = 0; i < 2; i++) {
auto ft = getFutureForDigest();
std::cout << std::boolalpha << ft.valid() << std::endl;
futures.push_back(std::move(ft));
}
std::cout << "futures size is " << futures.size() << std::endl;
// std::future<std::optional<BlockDigest>> f = futures.pop_back(); // does not work
auto f = std::async([]() { return getDigest(); });
if (f.valid()) {
auto oopt = f.get();
if (oopt.has_value()) {
auto digest = oopt.value();
for (auto d : digest) {
std::cout << d;
}
std::cout << "\n";
}
}
return 0;
}