In the code below, I am trying to return from a coroutine std :: vector . The problem is that only the result of the std::vector<int> get_return_object() is returned from the coroutine.
When the co_return statement is executed, method void return_value(std::vector<int>&& value) is called, which fills the vector returned by method get_return_object(). But the vector from get_return_object() is returned by value and it doesn't work. When I try to do so
std::vector<int> return_value(std::vector<int>&& value){
return value
}
An empty vector is also returned from the coroutine, although value is not empty.
How to return a value from a coroutine without wrapping it in a Task containing a promise_object?
exemple:
#include "coroutine"
#include "iostream"
struct Promise {
std::vector<int> vec;
std::vector<int> get_return_object() {
return vec;
}
std::suspend_never initial_suspend() {
return {};
}
std::suspend_never final_suspend() {
return {};
}
void return_void() {}
void return_value(std::vector<int>&& value){
vec = std::move(value);
}
void unhandled_exception() { std::terminate(); }
};
template<typename... Args>
struct std::coroutine_traits<std::vector<int>, Args...>{
using promise_type = Promise;
};
class Caller{
public:
std::vector<int> call();
};
std::vector<int> Caller::call() {
co_return std::vector<int>{1, 2, 3, 4};
}
int main(){
Caller c;
auto vec = c.call();
std::cout << vec.size();
return 0;
}