I have read in cppreference.com that the new(since C++17) std::vector::emplace_back
has an return value of referance to the inserted element.
Return value
- (none) (until C++17)
- A reference to the inserted element. (since C++17)
I was thinking, while inserting element to the vector, why we need a referance to it? how this could be usful or what is the usecase case of this new return?
Here is a sample code which I wrote to see, the feature.
#include <vector>
int main()
{
std::vector<int> myVec;
for(int i = 0; i < 3; ++i)
{
int& newElement = myVec.emplace_back(i);
^^^^^^^ => why standard should expose the element after inserting.
}
}
The change is made by P0084. The motivation the author gives is