MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / push_back

Method push_back

Examples/NoModules/Chapter 18/Ex18_05B/Array.h:128–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

126// push_back() overload for lvalue references
127template <typename T>
128void Array<T>::push_back(T element) // Pass by value (copy of lvalue, or moved rvalue!)
129{
130 Array<T> newArray{m_size + 1}; // Allocate a larger Array<>
131 for (size_t i{}; i < m_size; ++i) // Move all existing elements...
132 newArray[i] = std::move(m_elements[i]);
133
134 newArray[m_size] = std::move(element); // Move the new one (could itself be a copy already)...
135
136 swap(newArray); // ... and swap!
137}
138
139#endif

Callers 1

mainFunction · 0.45

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected