I have a specific use case:
In my library, concurrencpp, I want to implement the function make_exceptional_lazy_result
which is basically like the C# method Task.FromException
.
Inside the implementation of this function, I will always throw an exception, I will never co_return
a valid object.
My current implementation looks like this:
template<class type, class exception_type>
lazy_result<type> make_exceptional_lazy_result(exception_type exception) {
throw exception;
co_return *static_cast<type*>(std::malloc(sizeof(type)));
}
I'm grossed by the malloc
hack - In one hand, I have to co_return
something, on the other hand, the program will never actually reach this line.
Can I do better? I'm worried about returning a reference to an uninitialized local memory (I don't know if according to the language it's ok).
What other options can I do here that look better? Note that type
is not necessarily default-constructable, so returning a type{}
is not an option.
You can write your own awaitable type which throws the exception when
co_await
ed.