std::inplace_vector<T,N>::try_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::try_emplace_back
template< class... Args >
constexpr pointer try_emplace_back( Args&&... args );
(since C++26)
constexpr pointer try_emplace_back( Args&&... args );
Conditionally appends an object of type T
to the end of the container.
If size() == capacity() is true, there are no effects. Otherwise, appends direct-non-list-initialized with std::forward <Args>(args)... object of type T
.
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
[edit] Return value
std::addressof (back()) if size() < capacity(), nullptr otherwise.
[edit] Complexity
Constant.
[edit] Exceptions
Any exception thrown by initialization of inserted element. If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).
[edit] Notes
This section is incomplete
Reason: Explain the purpose of this API.
Reason: Explain the purpose of this API.
[edit] Example
Run this code
#include <cassert> #include <complex> #include <inplace_vector> int main() { using namespace std::complex_literals; using C = std::complex <double>; using I = std::inplace_vector <C, 3>; auto v = I{1.0 + 2.0i, 3.0 + 4.0i}; C* c = v.try_emplace_back(5.0, 6.0); assert (*c == 5.0 + 6.0i); assert ((v == I{1.0 + 2.0i, 3.0 + 4.0i, 5.0 + 6.0i})); c = v.try_emplace_back(7.0, 8.0); // no space => no insertion assert (c == nullptr); assert ((v == I{1.0 + 2.0i, 3.0 + 4.0i, 5.0 + 6.0i})); }