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

Method push_back

Examples/NoModules/Chapter 18/Ex18_07/Array.h:127–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls 1

swapFunction · 0.70

Tested by

no test coverage detected