rstl: Return reference to emplaced element in emplace_back()

Follows the behavior of the C++17 variant of emplace_back, which returns
a reference to the emplaced element.

This allows eliminating cases like:

container.emplace_back(some, things, arguments);
container.back().blah();

... // subsequent modifications to the element

given we can just retrieve the reference from the emplace_back call.
This commit is contained in:
Lioncash 2019-10-19 01:35:10 -04:00
parent 6a3eba0091
commit 15e856404d
1 changed files with 5 additions and 2 deletions

View File

@ -385,15 +385,18 @@ public:
} }
template <class... _Args> template <class... _Args>
void emplace_back(_Args&&... args) { T& emplace_back(_Args&&... args) {
#ifndef NDEBUG #ifndef NDEBUG
if (x0_size == N) { if (x0_size == N) {
Log.report(logvisor::Fatal, fmt("emplace_back() called on full rstl::reserved_vector.")); Log.report(logvisor::Fatal, fmt("emplace_back() called on full rstl::reserved_vector."));
} }
#endif #endif
::new (static_cast<void*>(std::addressof(_value(x0_size)))) T(std::forward<_Args>(args)...); T& element = _value(x0_size);
::new (static_cast<void*>(std::addressof(element))) T(std::forward<_Args>(args)...);
++x0_size; ++x0_size;
return element;
} }
void pop_back() { void pop_back() {