I can't figure out how to read this. How would I find out were _M_emplace_aux is defined? |
There's nothing like a decent IDE for navigating through code. Set up your editor so that it can bring you to _M_emplace_aux when you click on it.
I'd suggest carefully studying
std::vector's implementation. Start with the vector class itself, examine its data members and base classes. This info -- its representation, not a list of member functions -- is often the best way to figure out what a class does & how data moves through it.
The base class is present for exception-safety reasons; it manages memory, following a well-aged idiom.
This can be a tough exercise depending on your experience level, but the answers to the inevitable questions
"why is
this done"
are very enlightening. There's quite little left to opinion or chance, while the standard library implementers address issues that aren't typically discussed in class or beginner's books.
Alternatively, students have written relatively poor imitations of the real thing as an exercise for years -- and some examples should be easy to find on the web.
Most often these prototypes
a.) require much from the contained type (e.g. a default constructor)
b.) are not exception-safe
c.) do not support allocators
d.) are oversized
etc.
If
vector::allocator_type is
std::allocator, then
emplace_back is
roughly equivalent to this (untested) code
1 2 3 4 5 6 7 8 9
|
template <typename... Args>
void emplace_back(Args&&... args)
{
if (size == capacity)
increase_capacity();
new (buffer + size) value_type(std::forward<Args>(args)...);
size++;
}
|