Say I have a std::vector
that is declared in a loop's body and co_yield
ed:
some_generator<std::vector<int>> vector_sequence() {
while (condition()) {
std::vector<int> result = get_a_vector();
result.push_back(1);
co_yield std::move(result); // or just: co_yield result;
}
}
Quite obviously, result
isn't going to be used again after being co_yield
ed (or I am horribly mistaken), so it would make sense to move it. I tried co_yield
ing a simple non-copyable type without std::move
and it did not compile, so in generic code, one would have use std::move
. Does the compiler not recognize this (compiler bug?) or is it intended by the language that co_yield
always copies an lvalue, so I have to std::move
? I know that return
ing an lvalue that is a local variable looks like a copy but is guaranteed to be a move or an even better kind of copy elision, and this does not seem so much different from it.
I have read C++: should I explicitly use std::move() in a return statement to force a move?, which is related to this question, but does not answer it, and considered co_return vs. co_yield when the right hand side is a temporary, which as far as I understand, is not related to this question.