1

Background:

I'm trying to create a few of my own wrapper classes for the STL containers so I can separate implementation from my code base. I have already done alittle bit with my Vector class wrapper like so:

Vector.h

template<typename type>
    class Vector
    {
    public:
        Vector();
        Vector(std::initializer_list<type> initializer);
        Vector(int size, int defaultValue);
        Vector(int size);
        ~Vector();

        void PushBack(type itemToPushBack);
        type AtPosition(int position);

    private:
        std::vector<type> m_collectionOfItems;
    }; 

As you can see, I have constructors setup and I've used std::vector as a member so that I can just call std::vector functions within my own Vector classes.

Issue:

With std::array I have to specificy a size immediately when instantiating any object. So if I created a member variable like I did with my vector class, I would have to give that array object a size. I would rather the size be specified by the user using some similar constructor setup to Vector's (ex. MyArrayClass myArray(10) ). How might I try and implement this Array wrapper?

4
  • std::array wraps a static array, so its size must be specified at compile-time. A std::vector is an array whose size is determined dynamically at run-time. Commented Nov 27, 2016 at 4:42
  • 1
    The whole point of std::array, its chief distinction from std::vector, is that its size is determined at compile time and thus it doesn't require a dynamic memory allocation. If you insist on wrapping std::array, your wrapper should have the same property, otherwise it would largely defeat the point. Commented Nov 27, 2016 at 4:42
  • The standard containers are already designed to be as interchangeable as possible. Your time may be better served learning what generic functions are available to make container code transferable between containers. If it's not possible using the current standard library functions, the chances are it's not doable. Commented Nov 27, 2016 at 5:04
  • alloca and it's variants are non standard and should be avoided if at all possible but one can wrap it similarly. It's available on most compilers but it is NOT standard C++. Commented Nov 27, 2016 at 5:12

1 Answer 1

1

I would rather the size be specified by the user using some similar constructor setup to Vector's (ex. MyArrayClass myArray(10) ). How might I try and implement this Array wrapper?

The purpose of std::array, unlike std::vector, is that the size is specified at compile time. Its underlying structure is a plain vanilla array which needs the size at compile time.

I can think of the following wrapper in case it is useful

template <typename T, std::size_t N>
class Array {
  public:
    // stuff
  private:
    std::array<T, N> array_;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.