mercredi 8 mars 2017

std::vector for making it fixed size at runtime?

I need something that works like std::experimental::dynarray, so an array which size is decided at runtime.

So I thought about using a wrapper class for std::vector, giving all its features, but without the possibility to call resize, reserve or push_back. In few words, all the methods to change its size (please remember me if I missed some of them).

So I started writing this class:

CCVector.hpp:

template <typename T>
class CCVector{
public:
    CCVector(size_t size);
    T &operator[](typename std::vector<T>::size_type idx);
private:
    std::vector<T> v;
};

CCVector.cpp:

template<typename T>
CCVector<T>::CCVector(size_t size) : v(size){}
template<typename T>
T& CCVector<T>::operator[](typename std::vector<T>::size_type idx){
    return  v[idx];
}

But I this point I thought I have to re-implement every method of std::vector that I need! For example begin, end, size etc, and I don't know how to implement all of them...Besides, this is really bad for maintenance: as soon as I need a new method from std::vector I need to re-implement it in CCVector.

All of this because I want fixed size arrays at runtime. How can I solve this without using the non-standard std::experimental::dynarray?

Aucun commentaire:

Enregistrer un commentaire