Given the classes
struct Data
{
void bar() const;
void baz();
}
class Foo
{
std::vector<Data> data;
std::map<size_t, Data> indexed_data;
}
I'd like to implement something in class Foo so that I can do the following:
int main()
{
Foo foo;
for(const auto& data : foo.data())
data.bar();
for(auto& data : foo.indexed_data())
data.baz();
const auto& foo_ref = foo;
for(auto& data : foo_ref.data())
data.baz(); // constness violated, shouldn't compile
}
However, I don't wanna expose the class internals by just returning references to the containers. I might also work with classes where the range I'd like to iterate over isn't implemented as a container. So I basically want to create some sort of proxy object which is just a little more than a wrapper to a begin/end pair so that I can iterate over multiple things inside my class. Additionally I would like it to be const correct as displayed above. Is there any well-known pattern to realize this?