I have a class that contains some standard container that I want to return in a method, like so (just an example):
class IntArray
{
public:
IntArray(const vector<int>& vals) : vals(vals) {}
const vector<int>& getValues() const { return vals; }
vector<int>& getValues() { return vals; }
private:
vector<int> vals;
};
I returned the vector by reference to avoid making a copy of it (I would rather not want to rely on RVO). I don't want to do it using OutputIterators, because I really want to keep it short with C++11 range-based for loops like so:
for (int val : arr.getValues()) {
// Something
}
But say I want to change the type of the member variable to list<int>, then I would have to change the method's return type, too, which might lead to incompatibilities. I also don't want to implement begin() and end() methods because there might be more than one such container per class.
What would be the preferred way of doing this?
std::beginandstd::end, or which have abeginandendmember functions, will be supported by the ranged-basedforloop. Of course this includes all standard containers.