Reputation: 13
I am writing my own version of Standard library in C++. When it comes to the emplace_back
function in vector, I see signature like the following.
template
<class... Args>
void emplace_back (Args&&... args);
I thought it can accept multiple elements and emplace them at once at first because that's one benefit for using parameter pack, but the description shows that the function only emplace one item at a time. I look at other implementations of this function and they also just handled one item.
Why does C++ use parameter pack in this case?
Upvotes: 0
Views: 666
Reputation: 238311
Why emplace_back use parameter pack as argument?
Because classes can have constructors that in general may accept any number of arguments. In order to forward any number of arguments, we need to use a variadic argument pack.
I thought it can accept multiple elements
It does not. It accepts zero or more arguments for the constructor of single element.
Upvotes: 1