this is not a specific bug but a general C++ design question. Consider the following scenario:
struct MyInterface {};
struct Data : MyInterface{};
struct DataWrapper
{
SomeContainerOf<MyInterface*> getData()
{...}
private:
std::vector<Data> dataStorage;
};
In terms of my requirements, I need the accessor function for DataWrapper
to return a container of MyInterface*
values (pointers because we are really returning Data
values).
Ideally, we want to do this without making a copy. I want to know the best way to do this. From what I can tell, something like follows is the idiomatic C++ way to do this:
auto DataWrapper::getData()
{
return dataStorage | std::ranges::views::transform([](Data& d) -> MyInterface*
{
return &d;
});
}
But the problem is that it is really clear that this accessor method is explicit about its return values – I need it to be known that this method returns this list, and I’m worried that in making the return type auto
, it obstructs this a bit. It seems like (I could be wrong here) you aren’t supposed to know what the type of a view
is (in a similar style to lambdas)
The alternative to this that I’m considering would be to make a custom view (where I could make the type a little more clear), but it really pains me to reimplement something that is so clearly in the standard. What would be the recommended move for such a scenario? Does modern C++ say that I should keep the return value auto
?