What modern day characteristics you want to see in C++?

[ad_1]

@qMopey, what do you believe about std::array? It really is merely a effectively identified C array, wrapped in a tiny factor that adds the length as compile time continuous. Maintaining all the facts about the array collectively, alternatively of obtaining the classic error of employing the incorrect size and going out of bounds.

I believe the thought is great, and the want for an array variety is an vital function missing from C/C++.

If I poke into the header as implemented by MSVC I see some points that I never like, which means I never like how it is implemented. Which is a various subject than regardless of whether or not one thing like std::array is necessary or could be great (of course it is necessary and could be great!).

// At the top rated off &ltarray&gt header …
#incorporate &ltalgorithm&gt
#incorporate &ltiterator&gt
#incorporate &lttuple&gt

These are quite large headers that pull in other quite large headers, that pull in other quite large headers. This implementation is pretty objectively terrible for this purpose alone. All that code, and for what? An array that could be implemented sufficiently in 10 lines of transportable C++.

Yet another qualm I have is how the size of the array is templated and the memory for the array is stored straight inside the array itself. Personally if I had been to implement a templated array I would do it additional like this:

template &lttypename T&gt
struct Array
{
    size_t capacity
    T* information
}

And that is about it. Possibly some comfort member functions for points like array.start() and array.finish(), but I would not even implement iterators. Yes, STL has lots of properly implemented and beneficial generic algorithms like sorting and whatnot, which make use of iterators, but even so there is nothing at all incorrect with a pointer. array.information + i operates just fine, and generates much less templated code compared to iterator implementations (substantially decrease compile occasions).


Then there is the query of “what about `Array` vs `ArrayView`”, exactly where Array would be like std::array and shop components straight, and ArrayView would be like my instance of just a pointer to the underlying information. My argument would be to only have Array, and no by-worth semantics at all. The instance I wrote above can deal with all use circumstances just fine. Storing components straight in the array is, in my opinion, additional expressive than essential.

In my personal game I use an array, but it is essentially just a stripped down version of std::vector. The API appears like this.

template
struct array
{
    array()
    explicit array(void* user_allocator_context)
    explicit array(int capacity, void* user_allocator_context)
    ~array()

    T&amp add()
    T&amp add(const T&amp item)
    T&amp insert(int index)
    T&amp insert(int index, const T&amp item)
    void set(int index, const T&amp item)
    void get rid of(int index)
    T&amp pop()
    void unordered_get rid of(int index)
    void clear()
    void make certain_capacity(int num_components)
    void steal_from(array&ltT&gt* steal_from_me)

    int capacity() const
    int count() const

    T&amp operator[](int index)
    const T&amp operator[](int index) const

    T* operator+(int index)
    const T* operator+(int index) const

    array&ltT&gt&amp operator=(array&ltT&gt&amp rhs)

    T&amp final()
    const T&amp final() const

    T* information()
    const T* information() const

private:
    int m_capacity =
    int m_count =
    T* m_products = NULL
    void* m_mem_ctx = NULL
}

No want for iterators. Is compatible with std::sort. The only factor it can not do, that I often want it could, is to refer to memory elsewhere without having attempting to get in touch with `free` upon destruction (like the above `Array` instance). But, I by no means actually run into this use-case in practice.

[ad_2]

Latest posts