I want to mix up the co_yielding string literals and std::strings
Generator<std::string_view> range(int first, const int last) {
while (first < last) {
char ch = first++;
co_yield " | ";
co_yield std::string{ch, ch, ch};
}
}
However, I'm wondering about the lifetime of the std::string?
Maybe it's safe if you know you are going to consume the string_view
immediately?
for(auto sv : range(65, 91))
std::cout << sv;
https://godbolt.org/z/d5eoP9aTE
You could make it safe like this
Generator<std::string_view> range(int first, const int last) {
std::string result;
while (first < last) {
char ch = first++;
co_yield " | ";
result = std::string{ch, ch, ch};
co_yield result;
}
}