AM Z
AM Z

Reputation: 431

Can someone please explain this line of code

I was watching a youtube video on how to implement your own vector/dynamic_array in C++. I understood everything except one like of code, I'm new to c++ and trying to understand the underline data structure implementation. But I didn't see such a line of code like that one. The Code:

template<typename... Args>
T& EmplaceBack(Args&&... args) {
    if (m_Size >= m_Capacity) {
        ReAlloc(m_Capacity + m_Capacity / 2);
    }
    new(&m_Data[m_Size])T(std::forward<Args>(args)...); 
    return m_Data[m_Size++];
}

The line I didn't understand:

new(&m_Data[m_Size])T(std::forward<Args>(args)...);

What that line of code is really doing ? Btw, I don't really know what std::forward is. Thx for any explanation. :)

Upvotes: 1

Views: 305

Answers (1)

Vasilij
Vasilij

Reputation: 1941

This line of code constructs an object of type T with arguments args... at the address &m_Data[m_Size]. No memory allocation involved.

new(&m_Data[m_Size])T(std::forward<Args>(args)...);

Lets assume we call a "normal" new operator:

new T((std::forward<Args>(args)...);

This will allocate memory which size will be equal to sizeof(T) and call a constructor of type T at that memory address. std::forward is used in the technique called perfect forwarding. In short: no copies, no moves, no passing in any way - arguments are just given to the constructor as if you called it directly.

But what if you have your memory already preallocated - the main performance boosting point of many containers, like the vector? Placement new operator is your friend here. You provide it with the address of the preallocated memory and it only constructs the object. And no delete is required later, because you didn't allocate anything with placement new!

What about the destruction? Normally destructors are called automatically when you delete the object. In this case you'll have to call the object's destructor directly, like this - t->~T();

Upvotes: 3

Related Questions