std::optional
std::optional is a C++17 standard library utility that represents an object that may or may not contain a value. It is a type-safe alternative to using special values (like nullptr or -1) to indicate “no result.”
You can find some examples here[https://github.com/blue-stone-j/example/blob/main/cpp/src/std/optional.cpp].
std::future
std::future is a C++ standard library class template (in
std::promise
std::promise is a synchronization primitive in the C++ Standard Library (
| Promise Action | Future Reaction |
|---|---|
set_value(v) |
get() returns v |
set_exception(e) |
get() throws e |
| Destructor without setting | get() throws std::future_error |
| Value not ready yet | get() blocks until ready |
| Feature | std::promise |
std::async |
|---|---|---|
| Who manages the thread | You manage manually | Automatically creates/manages thread |
| When value is produced | When you call set_value() |
When async task completes |
| Exception handling | You must call set_exception() manually |
Automatically captured |