std::inplace_vector<T,N>::emplace_back
From cppreference.com
 
 
 < cpp | container | inplace vector 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Containers library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
 Tables
std::inplace_vector 
 
 
 
inplace_vector::emplace_back
template< class... Args >
constexpr reference emplace_back( Args&&... args );
 
 (since C++26) 
constexpr reference emplace_back( Args&&... args );
Appends a new element to the end of the container. Typically, the element is constructed using placement-new to construct the element in-place at the location provided by the container. The arguments args... are forwarded to the constructor as std::forward <Args>(args)....
No iterators or references are invalidated, except end(), which is invalidated if the insertion occurs.
[edit] Parameters
 args
 -
 arguments to forward to the constructor of the element
 Type requirements
 -
T must meet the requirements of EmplaceConstructible.
[edit] Return value
back(), i.e. a reference to the inserted element.
[edit] Complexity
Constant.
[edit] Exceptions
- std::bad_alloc if size() == capacity() before invocation.
- Any exception thrown by initialization of the inserted element.
If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).
[edit] Example
Run this code
#include <inplace_vector> #include <new> #include <print> #include <string> #include <utility> int main() { std::inplace_vector <std::pair <std::string, std::string >, 2> fauna; std::string dog{"\N{DOG}"}; fauna.emplace_back("\N{CAT}", dog); fauna.emplace_back("\N{CAT}", std::move(dog)); std::println ("fauna = {}", fauna); try { fauna.emplace_back("\N{BUG}", "\N{BUG}"); // throws: there is no space } catch(const std::bad_alloc & ex) { std::println ("{}", ex.what()); } std::println ("fauna = {}", fauna); }
Possible output:
fauna = [("🐈", "🐕"), ("🐈", "🐕")]
std::bad_alloc
fauna = [("🐈", "🐕"), ("🐈", "🐕")]