This example program does not compile, because the transform_view
cannot be converted to a std::span
:
class Foo {
private:
std::vector<std::string> strings = { "a", "b", "c" };
public:
std::span<const char*> getStrings() {
return strings | std::views::transform([](const std::string& str) { return str.c_str(); });
}
};
int main() {
Foo foo;
auto strings = foo.getStrings();
for (auto s : strings)
std::cout << s << std::endl;
}
I know that it is not possible to construct containers (like std::vector
) yet, however I don't quite understand, why it is not possible to construct a std::span
from it. I found this answer, that stated, that currently the only container that can be constructed from an arbitrary range is std::span
, so I expected the above example to work.
Is there any way to create a span from a range? Or is there any other way to return a generic view from a method, without using auto
(which is not allowed for virtual methods)?