Assume I have a member variable std::vector<std::string> in a class and I want to return it from a member function as an immutable view using a combination of gsl::array_view and gsl::cstring_view. Unfortunately, the following doesn't compile:
class C {
public:
gsl::array_view<const gsl::cstring_view<>> getVectorOfStrings() const
{
return _vectorOfStrings;
}
private:
std::vector<std::string> _vectorOfStrings;
};
The reason for this is that there's no container of cstring_view that the array_view can be created from. So my question is: is there a way to use such a construct without explicitly adding something like a member of type std::vector<gsl::cstring_view<>>, which is clearly undesirable?
Edit
It seems to me that such 'transforming' views might be of more general use. Consider having a vector of owning pointers, such as std::vector<std::shared_ptr<T>>, which I'd like to expose to the user of the class as an array_view of raw pointers: gsl::array_view<const T*> without exposing my implementation-defined storage approach. Thoughts?
array_viewofcstring_views, who would own the collection ofcstring_views, whose existence is required by the existence of thearray_viewof thecstring_views?array_viewwhich would convert whateverstring_view-compatible array value to astring_viewon request. I could probably write such a thing, but I thought that perhaps there's a better way. Thus this SO question :)cstring_views, may I ask why the avoidance of anarray_viewofstd::strings?std::vector<const char*>? It's kind of an 'academic' interest which might result in some contribution to gsl if other people deem it useful (who knows?).cstring_views and pass it out of the function to be converted as the client code sees fit. If the client needs it in exactly one expression, it can be returned as a temporary and the client can construct anarray_viewinline. Otherwise, they can just keep the vector and constructarray_views from it as needed. Of course you need to construct it every time you need it then, so it'd only make sense if you needed to pass anarray_view<cstring_view>into a function