Now the coro::task uses the std::optional to store the result, which is incorrect.
The optional is designed to store alterable values, so it requires that the type it stores is copy/move constructible and assignable. However, a normal function can definitely return a value with all these functions deleted.
Let's think about what do we need to return a value from the coro::task. First, the task needs to store the return value inside the promise, and then construct the return value in the caller's frame. So, the requirement of std::optional is too strong and does not meets what we need here. I suggest that we use a custom nullable type to replace this std::optional. A reference implementation is like this
And by the way, the empty or has_value flag can be combined with whether there is an exception flag. So that the std::exception_ptr can share the storage with the value. I suggest that we use this solution to further reduce the size of task. For example, when return type is uint64_t, std::exception_ptr + uint64_t + bool is acctually 24 bytes (because of padding), but if the std::exception_ptr and uint64_t shares storage, 8 + enum(unsigned char) is 16 bytes.
If you think these changes are good, I will soon create a new pull request which make these changes.
Now the
coro::task
uses thestd::optional
to store the result, which is incorrect.The optional is designed to store alterable values, so it requires that the type it stores is copy/move constructible and assignable. However, a normal function can definitely return a value with all these functions deleted.
Let's think about what do we need to return a value from the
coro::task
. First, the task needs to store the return value inside the promise, and then construct the return value in the caller's frame. So, the requirement ofstd::optional
is too strong and does not meets what we need here. I suggest that we use a custom nullable type to replace thisstd::optional
. A reference implementation is like thisAnd by the way, the empty or has_value flag can be combined with whether there is an exception flag. So that the
std::exception_ptr
can share the storage with the value. I suggest that we use this solution to further reduce the size of task. For example, when return type is uint64_t,std::exception_ptr
+uint64_t
+bool
is acctually 24 bytes (because of padding), but if thestd::exception_ptr
anduint64_t
shares storage, 8 + enum(unsigned char) is 16 bytes.If you think these changes are good, I will soon create a new pull request which make these changes.