This seems like a strange request. It would be interesting to know why you want this infinitely expanding vector. One solution (as suggested here) would be to inherit from std::vector
privately and implement your own resize, ie something like:
template <class T> // Can also forward the allocator if you want
class InfiniteResizeVector: private std::vector<T>
// The private is very important, std::vector has no virtual destructor, so you
// cannot allow someone to reference it polymorphically.
{
public:
using vector::push_back;
using vector::operator[];
... // For all the functions you need
void resize(size_t new_size) {
vector::resize(std::max(size(),new_size));
}
};
Usage would then just be v.resize(5);
like you asked for. Make sure you read some of the other answers from the link above. This is a very unusual thing to do, unless this is something you will use all the time it is certainly not worth making your own independent type.